【发布时间】:2016-09-01 11:09:25
【问题描述】:
我将查询字符串发布到 url,现在我想获取请求标头并使用其中一个键。我编写了一个可以获取响应标头但不能获取请求标头的代码。到目前为止,这是我的代码:
URL url = null;
try {
url = new URL(getString(R.string.auth_url));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
String urlParameters = "username="+ URLEncoder.encode(params[0], "UTF-8")+"&password="+URLEncoder.encode(params[1], "UTF-8")+"&captcha=1234";
con.setRequestProperty( "charset", "utf-8");
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(urlParameters);
writer.flush();
writer.close();
os.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
Log.i("body", "input = " + stringBuilder.toString());
reader.close();
return con.getHeaderFields().toString();
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
return con.getHeaderFields().toString() 行给了我整个响应头。现在我应该如何更改此代码以获取 请求标头?!
提示:我使用了 AsyncTask,以上几行在我的 doInBackground 方法中。
【问题讨论】:
标签: android http-post httprequest httpurlconnection request-headers