Java HttpClient returns "501 Method Not Implemented" and "POST to / not supported"
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.
精彩评论