2013-11-15 33 views
0

先生,我將通過httppost實現登錄模塊。該機制是,當我把 HTTP PARAMATERS到httppost並執行與URL http://api.apc.com/u/authorize, api.apc.com/auth/authorized?u=3cdndskjsijdso9808將返回HTTP POST失敗後Java GET重定向URL

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

回答

0

構建您的DefaultHttpClient時,這樣它不會自動執行重定向你需要設置PARAMS。

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(); 
} 
+0

爲什麼內存本身可以緩存這樣的重定向url並使標頭位置不可見? –

+0

仍然失敗,這是因爲沒有參數get的重定向url。我現在應該怎麼做 ? –

+0

請將更新的代碼添加到您的原始帖子。 –