【问题标题】:Java GET redirected URL after HTTP Post exceute failedHTTP Post 执行失败后 Java GET 重定向 URL
【发布时间】:2013-11-15 18:03:02
【问题描述】:

先生,我将通过httppost实现登录模块。机制是当我 将 http 参数放入 httppost 并使用 url http://api.apc.com/u/authorize 执行, api.apc.com/auth/authorized?u=3cdndskjsijdso9808 将被返回

u= 3cdndskjsijdso9808 是登录成功后的用户token。

打印了数千个标题字段,但找不到标题“位置” 在android设备上,但在实现和测试时可以找到PC浏览器。

请问是否有其他方法可以获取 http 响应标头的属性和值?

以下是我的代码:

HttpClient client = new DefaultHttpClient();        
String url =                 "https://api.hkgalden.com/u/authorize";

HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("X-GALAPI-KEY", "ecb954a155e6bbd9fe3fe166940feb102c80ae90");
httpPost.setHeader("Cache-Control", "no-cache");
httpPost.setHeader("Pragma", "no-cache");

Log.v("HTTP", "Header Added");

try {
    //add HTTP parameters
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email", email));
    nameValuePairs.add(new BasicNameValuePair("appid", "27"));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    nameValuePairs.add(new BasicNameValuePair("deviceid", "mobile_id"));
    nameValuePairs.add(new BasicNameValuePair("dname", "Galden+ mobile device"));
    Log.v("HTTP", "Parameters Added");

    //send HTTP request

    //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/*
    String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");

    url += ("?" + paramString);
*/
    //execute HTTP response
/*
    HttpGet get = new                 HttpGet (url);
    get.addHeader("X-GALAPI-KEY", "ecb954a155e6bbd9fe3fe166940feb102c80ae90");
    get.setHeader("Cache-Control", "no-cache");
    get.setHeader("Pragma", "no-cache");*/


    final     HttpParams httpParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParams, false);                    
    HttpResponse httpResponse = client.execute(httpPost);
    //HttpResponse httpResponse = client.execute(get);
    Log.v("HTTP", "Request Executed");
    //if execute success
    int respondCode = httpResponse.getStatusLine().getStatusCode();
    Log.d("HTTP Status Code" , String.valueOf(respondCode));
    if (respondCode== 404  || respondCode == 200){
        SystemUtils.toast(getApplication(), "Login Success!");
        // String temp = httpResponse.getStatusLine().toString();
        // String temp = httpResponse.getLastHeader("Location").getValue();
        // String temp = httpResponse.getHeaders();
        // Log.v("URL", temp);
        Header[] headers = httpResponse.getAllHeaders();
        for (Header header : headers) {
            Log.v(header.getName(), header.getValue());
        }
        for(Header header : httpResponse.getHeaders("Location")) {
            System.out.println("Location from connect:" + header.getValue());
            SystemUtils.toast(getApplication(), "URL : " +  header.getValue());
        }

        String data = slurp(httpResponse.getEntity().getContent() , 1024);
        Log.v("url", data);
    }
    else SystemUtils.toast(getApplication(), "Login Fail! Error code: " + Integer.toString(httpResponse.getStatusLine().getStatusCode()));
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

【问题讨论】:

    标签: android http-headers apache-httpclient-4.x


    【解决方案1】:

    您需要在构造 DefaultHttpClient 时设置参数,使其不会自动跟随重定向。

    HttpParams httpParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParams, false);
    HttpResponse httpResponse = new DefaultHttpClient(httpParams).execute(httpPost);
    

    这主要来自我的回忆,但我似乎记得默认情况下 HttpClient 将遵循重定向,除非明确告知不要这样做。

    编辑提交者没有正确遵循我的原始说明,因此我复制了提交的代码,并附上了建议的修复方法,并澄清了不应放置的位置。

    /*
     * THIS is the location and manner I suggested using the http params with 
     * the DefaultHttpClient. I would suggest you read my comments further 
     * down in your code.
     */
    final     HttpParams httpParams = new BasicHttpParams();
    HttpClientParams.setRedirecting(httpParams, false);                    
    HttpClient client = new DefaultHttpClient(httpParams);
    String url = "https://api.hkgalden.com/u/authorize";
    
    HttpPost httpPost = new HttpPost(url);
    httpPost.addHeader("X-GALAPI-KEY", "ecb954a155e6bbd9fe3fe166940feb102c80ae90");
    httpPost.setHeader("Cache-Control", "no-cache");
    httpPost.setHeader("Pragma", "no-cache");
    
    Log.v("HTTP", "Header Added");
    
    try {
        //add HTTP parameters
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("email", email));
        nameValuePairs.add(new BasicNameValuePair("appid", "27"));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        nameValuePairs.add(new BasicNameValuePair("deviceid", "mobile_id"));
        nameValuePairs.add(new BasicNameValuePair("dname", "Galden+ mobile device"));
        Log.v("HTTP", "Parameters Added");
    
        //send HTTP request
    
        //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    /*
        String paramString = URLEncodedUtils.format(nameValuePairs, "utf-8");
        url += ("?" + paramString);
    */
        //execute HTTP response
    /*
        HttpGet get = new                 HttpGet (url);
        get.addHeader("X-GALAPI-KEY", "ecb954a155e6bbd9fe3fe166940feb102c80ae90");
        get.setHeader("Cache-Control", "no-cache");
        get.setHeader("Pragma", "no-cache");
    */
    
    
        /*
         * I have commented the below declaration that I had give you to indicate 
         * where it was that you claimed you used it THEN claimed it didn't work.
         *
         * I also want to point out that my suggestion indicated originally to
         * take the httpParams object and pass it as a constructor argument to the
         * DefaultHttpClient.
         *
         */
        /*
        final     HttpParams httpParams = new BasicHttpParams();
        HttpClientParams.setRedirecting(httpParams, false);                    
        */
        HttpResponse httpResponse = client.execute(httpPost);
        //HttpResponse httpResponse = client.execute(get);
        Log.v("HTTP", "Request Executed");
        //if execute success
        int respondCode = httpResponse.getStatusLine().getStatusCode();
        Log.d("HTTP Status Code" , String.valueOf(respondCode));
        if (respondCode== 404  || respondCode == 200){
            SystemUtils.toast(getApplication(), "Login Success!");
            // String temp = httpResponse.getStatusLine().toString();
            // String temp = httpResponse.getLastHeader("Location").getValue();
            // String temp = httpResponse.getHeaders();
            // Log.v("URL", temp);
            Header[] headers = httpResponse.getAllHeaders();
            for (Header header : headers) {
                Log.v(header.getName(), header.getValue());
            }
            for(Header header : httpResponse.getHeaders("Location")) {
                System.out.println("Location from connect:" + header.getValue());
                SystemUtils.toast(getApplication(), "URL : " +  header.getValue());
            }
    
            String data = slurp(httpResponse.getEntity().getContent() , 1024);
            Log.v("url", data);
        }
        else SystemUtils.toast(getApplication(), "Login Fail! Error code: " + Integer.toString(httpResponse.getStatusLine().getStatusCode()));
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

    • 为什么内存本身可以缓存这样的重定向 url 并使标头位置不可见?
    • 仍然失败,这是因为没有带有参数 get 的重定向 url。我现在该怎么办?
    • 它仍然失败,因为 http post 后“位置”标头不存在
    • 11-16 22:19:12.988: V/Connection(31241): 保持活动状态 11-16 22:19:12.988: V/Set-Cookie(31241): __cfduid=d62653256fbe8bbbcbf2bb52336d9a91713846116;过期=格林威治标准时间 2019 年 12 月 23 日星期一 23:50:00;路径=/;域名=.hkgalden.com; HttpOnly 11-16 22:19:12.988: V/X-Powered-By(31241): PHP/5.5.4-1~dotdeb.1 11-16 22:19:12.988: V/Set-Cookie(31241): hkgapi_apigalden_session=a%3A5%3A%7Bs%3A10%3A%2... 11-16 22:19:12.988: V/CF-RAY(31241): ce4d326343000c5
    • 您更新的代码没有使用我提供的建议。
    猜你喜欢
    • 1970-01-01
    • 2022-12-16
    • 2012-12-11
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多