httpClient----Post 和 GET请求
2013-09-13 11:02
441 查看
在http请求失败是要进行下列操作:
[java] view
plaincopy
if (response.getStatusLine ().getStatusCode () != 200) {
get.abort();
return null;
}
HttpGet操作:
[java] view
plaincopy
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的:
[java] view
plaincopy
public class Demo1 {
/**
* 用 get 方法访问 www.apache.org 并返回内容
*
* @param args
*/
public static void main(String[] args) {
//创建默认的 HttpClient 实例
HttpClient httpClient = new DefaultHttpClient();
try {
//创建 httpUriRequest 实例
HttpGet httpGet = new HttpGet("http://www.apache.org/");
System.out.println("uri=" + httpGet.getURI());
//执行 get 请求
HttpResponse httpResponse = httpClient.execute(httpGet);
//获取响应实体
HttpEntity httpEntity = httpResponse.getEntity();
//打印响应状态
System.out.println(httpResponse.getStatusLine());
if (httpEntity != null) {
//响应内容的长度
long length = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
System.out.println("Response content length:" + length);
System.out.println("Response content:" + content);
}
//有些教程里没有下面这行
httpGet.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
httpClient.getConnectionManager().shutdown();
}
}
}
分析请求包中这六个头信息:httpget.setHeader("Accept-Encoding", "gzip, deflate")可能产生乱码
[java] view
plaincopy
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.iteye.com");
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");
httpget.setHeader("Accept-Encoding", "gzip, deflate");
httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
httpget.setHeader("Host", "www.iteye.com");
httpget.setHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
HttpPost操作:
[java] view
plaincopy
public class Demo2 {
/**
* 用post方法访问本地应用根据传递参数不同,返回不同结果
*
* @param args
*/
public static void main(String[] args) {
//创建默认的 HttpClient 实例
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet");
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("param1", "***"));
UrlEncodedFormEntity urlEncodedFormEntity;
try {
urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
System.out.println("execurting request:" + httpPost.getURI());
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String content = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println("Response content:" + content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
httpClient.getConnectionManager().shutdown();
}
}
}
[java] view
plaincopy
if (response.getStatusLine ().getStatusCode () != 200) {
get.abort();
return null;
}
HttpGet操作:
[java] view
plaincopy
public final static void main(String[] args) throws Exception {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.apache.org/");
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
}
读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的:
[java] view
plaincopy
public class Demo1 {
/**
* 用 get 方法访问 www.apache.org 并返回内容
*
* @param args
*/
public static void main(String[] args) {
//创建默认的 HttpClient 实例
HttpClient httpClient = new DefaultHttpClient();
try {
//创建 httpUriRequest 实例
HttpGet httpGet = new HttpGet("http://www.apache.org/");
System.out.println("uri=" + httpGet.getURI());
//执行 get 请求
HttpResponse httpResponse = httpClient.execute(httpGet);
//获取响应实体
HttpEntity httpEntity = httpResponse.getEntity();
//打印响应状态
System.out.println(httpResponse.getStatusLine());
if (httpEntity != null) {
//响应内容的长度
long length = httpEntity.getContentLength();
//响应内容
String content = EntityUtils.toString(httpEntity);
System.out.println("Response content length:" + length);
System.out.println("Response content:" + content);
}
//有些教程里没有下面这行
httpGet.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
httpClient.getConnectionManager().shutdown();
}
}
}
分析请求包中这六个头信息:httpget.setHeader("Accept-Encoding", "gzip, deflate")可能产生乱码
[java] view
plaincopy
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("http://www.iteye.com");
httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5");
httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");
httpget.setHeader("Accept-Encoding", "gzip, deflate");
httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");
httpget.setHeader("Host", "www.iteye.com");
httpget.setHeader("Connection", "Keep-Alive");
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
}
System.out.println("----------------------------------------");
InputStream inSm = entity.getContent();
Scanner inScn = new Scanner(inSm);
while (inScn.hasNextLine()) {
System.out.println(inScn.nextLine());
}
// Do not feel like reading the response body
// Call abort on the request object
httpget.abort();
} catch (Exception e) {
e.printStackTrace();
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpClient.getConnectionManager().shutdown();
}
}
HttpPost操作:
[java] view
plaincopy
public class Demo2 {
/**
* 用post方法访问本地应用根据传递参数不同,返回不同结果
*
* @param args
*/
public static void main(String[] args) {
//创建默认的 HttpClient 实例
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet");
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
formParams.add(new BasicNameValuePair("param1", "***"));
UrlEncodedFormEntity urlEncodedFormEntity;
try {
urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");
httpPost.setEntity(urlEncodedFormEntity);
System.out.println("execurting request:" + httpPost.getURI());
HttpResponse httpResponse = null;
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
String content = EntityUtils.toString(httpEntity, "UTF-8");
System.out.println("Response content:" + content);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭连接,释放资源
httpClient.getConnectionManager().shutdown();
}
}
}
相关文章推荐
- android HttpClient get请求与post请求工具类
- HttpUrlconnection 、Httpclient get 、post 请求核心代码
- HttpClient get、post请求
- [Socket]利用Android下的HttpClient发送GET && POST请求
- android HttpClient get请求与post请求工具类
- HttpClient 的post和get请求
- Java模拟HTTP的Get和Post请求(增强) Apache-httpclient
- HttpClient发送get请求,post请求,携带cookie访问,json提交
- HttpClient get和post请求的示例代码以及防乱码处理
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete
- Android HttpClient GET或者POST请求基本使用方法
- java apache commons HttpClient发送get和post请求的学习整理
- Android_HttpClient_get请求post表单提交上传
- go http.Get请求 http.Post请求 http.PostForm请求 Client 超时设置
- 【转】HttpClient GET和POST请求
- HttpClient之Get请求和Post请求示例 编辑
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- 用httpclient4.x 发送http get post请求。
- httpClient Post例子,Http 四种请求访问代码 HttpGet HttpPost HttpPut HttpDelete