/**
* 以Post的方式向服务器提交数据,只返回响应的代码
*
* @param url
* 请求的地址
* @param paramList
* 参数列表
* @return
*/
public static String post(String url, HashMap<String, String> paramList) {
if (url == null) {
throw new NullPointerException();
}
String html = null;
int resultCode = -1;
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); int dataSize = paramList.size();
Object[] keys = paramList.keySet().toArray();
for (int i = 0; i < dataSize; i++) {
String key = keys[i].toString();
params.add(new BasicNameValuePair(key, paramList.get(key)));
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse get_response = httpClient.execute(request); // 执行请求
HttpPost postMethod = new HttpPost(url);
Header[] headers = get_response.getAllHeaders();// 获取所有的头信息
for (Header header : headers) {
postMethod.addHeader(header.getName(),header.getValue());
}
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 将参数填入POST
// Entity中
HttpResponse post_response = httpClient.execute(postMethod); // 执行POST方法
resultCode = post_response.getStatusLine().getStatusCode();// 获取响应代码
System.out.println("相应代码" + resultCode);
if (resultCode == 200) {
html = EntityUtils.toString(post_response.getEntity(), "utf-8");// 获取响应内容
System.out.println(html);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return resultCode;
return html;
}大家看看上面的代码,为什么我在执行post之前设置了HTTP头信息之后就没法执行了呢?把设置HTTP头信息的部分去掉就没事,求解释
* 以Post的方式向服务器提交数据,只返回响应的代码
*
* @param url
* 请求的地址
* @param paramList
* 参数列表
* @return
*/
public static String post(String url, HashMap<String, String> paramList) {
if (url == null) {
throw new NullPointerException();
}
String html = null;
int resultCode = -1;
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); int dataSize = paramList.size();
Object[] keys = paramList.keySet().toArray();
for (int i = 0; i < dataSize; i++) {
String key = keys[i].toString();
params.add(new BasicNameValuePair(key, paramList.get(key)));
}
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse get_response = httpClient.execute(request); // 执行请求
HttpPost postMethod = new HttpPost(url);
Header[] headers = get_response.getAllHeaders();// 获取所有的头信息
for (Header header : headers) {
postMethod.addHeader(header.getName(),header.getValue());
}
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); // 将参数填入POST
// Entity中
HttpResponse post_response = httpClient.execute(postMethod); // 执行POST方法
resultCode = post_response.getStatusLine().getStatusCode();// 获取响应代码
System.out.println("相应代码" + resultCode);
if (resultCode == 200) {
html = EntityUtils.toString(post_response.getEntity(), "utf-8");// 获取响应内容
System.out.println(html);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return resultCode;
return html;
}大家看看上面的代码,为什么我在执行post之前设置了HTTP头信息之后就没法执行了呢?把设置HTTP头信息的部分去掉就没事,求解释
那我可不可设置HttpPost不要自动添加头信息?如果可以该怎么添加呢
哪些头信息不能放在POST里面呢
post本来就是通过消息体封装的