【发布时间】: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