HttpPost->重定向->需要回复的位置或正文

发布于 2024-12-13 18:39:41 字数 2003 浏览 2 评论 0原文

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

我尝试了一切来发布代码功能,但我就是做不到。

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<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()));

代码的作用:

  1. 帖子
  2. 被重定向-获取位置的标头
  3. 解析位置

我需要android做同样的事情。 “位置”或回复正文都可以,我不需要两者。

帖子: http://www.anddev.org/网络-数据库-问题-f29/httppost-clientprotocolexception-t56118.html

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涫野音 2024-12-20 18:39:41

我已经找到问题了!

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

只需更改这一行 - 版本 1_0 有效,而 1_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!

℡寂寞咖啡 2024-12-20 18:39:41

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

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

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);
我很坚强 2024-12-20 18:39:41

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

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

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

httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文