【问题标题】:After sending HTTP request to server sometimes I get status 302 (redirect)在向服务器发送 HTTP 请求后,有时我会得到状态 302(重定向)
【发布时间】:2013-07-02 11:21:00
【问题描述】:

我尝试使用 json 参数向某个服务器发送 POST 请求,然后等待 json 响应。

在 Android 上它工作正常,但在 Linux 上我得到状态 302 重定向。我真的不知道我的问题在哪里。

这是来自 linux 的命令:

curl -i -X POST --data "id=105&json={\"orderBy\":0\"maxResults\":50}" "http://mysite.com/ctlClient/"

我得到回应:

 HTTP/1.1 302 Found
 Date: Thu, 04 Jul 2013 12:22:06 GMT
 Server: Apache
 X-Powered-By: PHP/5.3.19
 Set-Cookie: PHPSESSID=dqblvijvttgsdrv2u5tn72p9d6; path=/
 Expires: Thu, 19 Nov 1981 08:52:00 GMT
 Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
 Pragma: no-cache
 location: http://mysite.com/fwf/online/
 Content-Length: 0
 Connection: close
 Content-Type: text/html

来自服务器access log

"POST /ctlClient/ HTTP/1.1" 302 - "-" "curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6 .5"

安卓:

            String data = "{\"orderBy\":0\"maxResults\":50}";
        String WEFI_MAIL_URL = "http://mysite.com/ctlClient/";


        DefaultHttpClient httpclient = null;
        String out = null;

        try {
            httpclient = new DefaultHttpClient();

            HttpPost httpost = new HttpPost(WEFI_MAIL_URL);

            httpost.setHeader("User-Agent", "Apache-HttpClient/4.1 (java 1.5)");

            List <NameValuePair> nvps = new ArrayList <NameValuePair>();
            nvps.add(new BasicNameValuePair("id", "105"));
            nvps.add(new BasicNameValuePair("json", data));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                InputStream is = entity.getContent();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                while ( (line = br.readLine()) != null) {
                    out = line;
                }
                is.close();

                if(isTimeOut == false){
                    _loadActual();
                }
                else{
                    return;
                }   
            } else {
                return;
            }

        } catch (Exception e) {             
        }

        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }       
    }

access log Android 回复:

 "POST /ctlClient/ HTTP/1.1" 302 - "-" "Apache-HttpClient/4.1 (java 1.5)"
 "GET /fwf/online/ HTTP/1.1" 200 13981 "-" "Apache-HttpClient/4.1 (java 1.5)"

我们可以看到在 POST 服务器上将我重定向到 /fwf/online/

从 Wireshark 中,我从两种方法中得到相同的结果:

POST /ctlClient/ HTTP/1.1
User-Agent: Apache-HttpClient/4.1 (java 1.5)
Content-Type: application/x-www-form-urlencoded
Content-Length: 301
Host: mysite.com
Connection: Keep-Alive

为什么它可以在 Android 上运行,但不能在带有 CURL 的 Linux 上运行?

谁能帮助我或指导我如何解决它?

谢谢

【问题讨论】:

    标签: android http post curl


    【解决方案1】:

    您想在 curl 命令中添加 -L。见http://curl.haxx.se/docs/faq.html#How_do_I_tell_curl_to_follow_HTT

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-25
      • 2016-05-03
      • 1970-01-01
      • 2016-12-11
      • 2017-01-28
      • 1970-01-01
      • 2012-09-15
      • 1970-01-01
      相关资源
      最近更新 更多