开发者

Java HttpClient returns "501 Method Not Implemented" and "POST to / not supported"

开发者 https://www.devze.com 2023-03-06 00:54 出处:网络
I am using HttpClient to submit a form to a webpage and when I look at HTTP header which browser is sending it is definitely sending POST request there.

I am using HttpClient to submit a form to a webpage and when I look at HTTP header which browser is sending it is definitely sending POST request there.

But when I try to do the same with HttpClient it returnes error page with the message in the subject.

HttpPost httpost = (HttpPost) prepareRequest(new HttpPost(targetURL));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpost);
HttpEntity entity = response.getEntity();
content = EntityU开发者_如何学运维tils.toString(entity);
EntityUtils.consume(entity);


501 could also mean that your Content type is not set correctly for some web-servers.

If you don't want to deal with this low-level API, this would be the code needed in Resty for a x-www-form-urlencoded POST.

Resty r = new Resty();
String result = r.text(targetUrl, form(nvps)).toString();

Disclaimer: I'm the author of Resty.

0

精彩评论

暂无评论...
验证码 换一张
取 消