HTTP Post执行失败后的Java GET重定向URL

时间:2013-11-15 18:03:02

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

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

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

打印出数千个标题字段,但未找到标题“位置” 在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();
}

1 个答案:

答案 0 :(得分:0)

在构建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();
}