2013-11-15 2 views
0

선생님, httppost로 로그인 모듈을 구현할 것입니다. 메커니즘은 내가 URL을 http://api.apc.com/u/authorize와 httppost로의 HTTP PARAMATERS를 넣고 실행 때, api.apc.com/auth/authorized?u=3cdndskjsijdso9808이HTTP Post Exceute 후 Java GET 리디렉션 된 URL

을 반환 할 것입니다

U = 3cdndskjsijdso9808 성공적으로 로그인 한 후 사용자 토큰입니다 .

수천 개의 헤더 필드가 출력되지만 안드로이드 장치에서는 헤더 "위치"가 발견되지 않고 이지만 구현 및 테스트에서는 PC 브라우저를 찾을 수 있습니다.

http 응답 헤더의 속성 및 값을 가져 오는 다른 방법이 있습니까? 자동 리디렉션을 따르지 않는 있도록 DefaultHttpClient를 구성 할 때

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

당신은 설정에 PARAMS가 필요합니다

은 아래에있는 내 코드입니다.

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

은 대부분 내 기억에서 오는,하지만 난 명시 적으로 지시가 없을 때는 기본적으로 리디렉션을 따를 HttpClient를 기억하는 것 같다 않습니다.

EDIT 제출자가 내 원래 지침을 올바르게 따르지 않아서 제출 된 코드를 권장 수정 사항과 함께 복제 할뿐만 아니라 제출하지 않을 위치에 대한 설명을 복제했습니다.

/* 
* 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

여전히 매개 변수가있는 리다이렉트 URL이 없기 때문에 실패합니다. 이제 어떻게해야합니까? –

+0

원래 게시물에 업데이트 된 코드를 추가하십시오. –