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();
}
爲什麼內存本身可以緩存這樣的重定向url並使標頭位置不可見? –
仍然失敗,這是因爲沒有參數get的重定向url。我現在應該怎麼做 ? –
請將更新的代碼添加到您的原始帖子。 –