HttpPost -> Redirect -> Location or body of response needed

本秂侑毒 提交于 2019-12-01 11:56:31

问题


Here is Java code that POSTs data to a website and than gets redirected as a response (status 302). It works perfectly on my PC (Eclipse, Java, Ubuntu), it does exactly what I want it to do.

I tried quite everything to post the code functionality but I just am not able to.

Java code:

// 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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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()));

Code does:

  1. Posts
  2. Gets Redirected - gets header of Location
  3. Parses the Location

And I need android to do the same. Either "Location" or body of repsonse, is ok, I dont need both.

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


回答1:


I have found the problem!

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

Just changing this one line - version 1_0 works and 1_1 does not. Don't ask me why :)

Thank you all!




回答2:


Please try the following code. The location in the header is missing, because the page has already redirected. So we can disable redirection to get the location tag.

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



回答3:


Try calling this after you create your http client so that it follows your redirect

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


来源:https://stackoverflow.com/questions/8014997/httppost-redirect-location-or-body-of-response-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!