HttpPost – >重定向 – >所需响应的位置或主体

以下是将数据POST到网站而不是作为响应重定向的Java代码(状态302)。 它在我的PC(Eclipse,Java,Ubuntu)上完美运行,它完全符合我的要求。

我尝试了很多东西来发布代码function,但我无法做到。

Java代码:

// Preparing the CLIENT and POST Method HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://na.leagueoflegends.com/ladders/solo-5x5"); try { // Add your POST METHOD attributes List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("op", "Search")); nameValuePairs.add(new BasicNameValuePair("player", "Jaiybe")); nameValuePairs.add(new BasicNameValuePair("ladder_id", "3")); nameValuePairs.add(new BasicNameValuePair("form_build_id", "form-526370b788622996caa3669e7b975ccf")); nameValuePairs.add(new BasicNameValuePair("form_id", "ladders_filter_form")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); // RESPONE THAT WORKS WITH JAVA System.out.println("Location:"); String LocationHeader = response.getFirstHeader("location").getValue(); System.out.println(LocationHeader); System.out.println(); // To get the BODY I would have to parse that again - since its not REDIRECTING automatically HttpClient httpclient2 = new DefaultHttpClient(); HttpPost httppost2 = new HttpPost(LocationHeader); response = httpclient2.execute(httppost2); System.out.println("And EVEN the response body:"); System.out.println(EntityUtils.toString(response.getEntity())); 

代码做:

  1. post
  2. 获取重定向 – 获取位置标题
  3. 解析位置

而且我需要android来做同样的事情。 无论是“位置”还是回复体,都可以,我不需要两者。

post: http : //www.anddev.org/networking-database-problems-f29/httppost-clientprotocolexception-t56118.html

我发现了问题!

 httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0); 

只需改变这一行 – 版本1_0有效,1_1没有。 不要问我为什么:)

谢谢你们!

请尝试以下代码。 缺少标头中的位置,因为页面已经重定向。 因此,我们可以禁用重定向以获取位置标记。

 httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, false); 

在创建http客户端后尝试调用此方法,以便它遵循重定向

 httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true);