HttpClient下载图片和向服务器提交数据实例
2016-08-18 20:53
288 查看
使用 HttpClient 需要以下 6 个步骤:
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
[java] view
plain copy
print?
public class DemoHttpClient03 {
public static void main(String[] args) throws ClientProtocolException, IOException {
//1,导包
//2,得到HttpClient对象
HttpClient client = new DefaultHttpClient();
//3,设置请求方式
HttpGet get = new HttpGet("http://photocdn.sohu.com/20150610/mp18368185_1433925691994_5.jpg");
//4,执行请求, 获取响应信息
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode() == 200)
{
//得到实体
HttpEntity entity = response.getEntity();
byte[] data = EntityUtils.toByteArray(entity);
//图片存入磁盘
FileOutputStream fos = new FileOutputStream("d:/mpl.jpg");
fos.write(data);
fos.close();
System.out.println("图片下载成功!!!!");
}
}
}
[java] view
plain copy
print?
public class DemoHttpClient04 {
public static void main(String[] args) throws ClientProtocolException, IOException {
//1, 导包
//2, 得到HttpClient对象
HttpClient client = new DefaultHttpClient();
//3, 设置请求方式 post
HttpPost post = new HttpPost("http://localhost:8080/Day_28_Servlet/LoginServlet");
//6, List<BasicNameValuePair>
List<BasicNameValuePair> parameters = new ArrayList();
BasicNameValuePair p1 = new BasicNameValuePair("useName", "abc");
parameters.add(p1);
BasicNameValuePair p2 = new BasicNameValuePair("usePwd", "123");
parameters.add(p2);
//5, 请求"实体" (封装请求参数的对象)
HttpEntity entity = new UrlEncodedFormEntity(parameters);
//4, 需要给post中加入参数
post.setEntity(entity);
//7, 执行请求, 获取响应
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() ==200)
{
//得到响应的实体
HttpEntity responseEntity = response.getEntity();
String str = EntityUtils.toString(responseEntity);
System.out.println("响应的内容为 : " + str);
}
}
}
1. 创建 HttpClient 的实例
2. 创建某种连接方法的实例,在这里是GetMethod。在 GetMethod 的构造函数中传入待连接的地址
3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例
4. 读 response
5. 释放连接。无论执行方法是否成功,都必须释放连接
6. 对得到后的内容进行处理
[java] view
plain copy
print?
public class DemoHttpClient03 {
public static void main(String[] args) throws ClientProtocolException, IOException {
//1,导包
//2,得到HttpClient对象
HttpClient client = new DefaultHttpClient();
//3,设置请求方式
HttpGet get = new HttpGet("http://photocdn.sohu.com/20150610/mp18368185_1433925691994_5.jpg");
//4,执行请求, 获取响应信息
HttpResponse response = client.execute(get);
if(response.getStatusLine().getStatusCode() == 200)
{
//得到实体
HttpEntity entity = response.getEntity();
byte[] data = EntityUtils.toByteArray(entity);
//图片存入磁盘
FileOutputStream fos = new FileOutputStream("d:/mpl.jpg");
fos.write(data);
fos.close();
System.out.println("图片下载成功!!!!");
}
}
}
[java] view
plain copy
print?
public class DemoHttpClient04 {
public static void main(String[] args) throws ClientProtocolException, IOException {
//1, 导包
//2, 得到HttpClient对象
HttpClient client = new DefaultHttpClient();
//3, 设置请求方式 post
HttpPost post = new HttpPost("http://localhost:8080/Day_28_Servlet/LoginServlet");
//6, List<BasicNameValuePair>
List<BasicNameValuePair> parameters = new ArrayList();
BasicNameValuePair p1 = new BasicNameValuePair("useName", "abc");
parameters.add(p1);
BasicNameValuePair p2 = new BasicNameValuePair("usePwd", "123");
parameters.add(p2);
//5, 请求"实体" (封装请求参数的对象)
HttpEntity entity = new UrlEncodedFormEntity(parameters);
//4, 需要给post中加入参数
post.setEntity(entity);
//7, 执行请求, 获取响应
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode() ==200)
{
//得到响应的实体
HttpEntity responseEntity = response.getEntity();
String str = EntityUtils.toString(responseEntity);
System.out.println("响应的内容为 : " + str);
}
}
}
相关文章推荐
- HttpClient下载图片和向服务器提交数据实例
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- android之向服务器提交数据(GET,POST,AsyncHttpClient)
- android HttpClient将数据提交到服务器
- 向服务器提交数据(GET-POST-AsyncHttpClient)
- HttpClient 使用get方式发送数据到服务器并下载服务器文件
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android-向服务器提交数据(GET-POST-AsyncHttpClient)
- 微信小程序服务器请求和上传数据,上传图片并展示,提交表单完整实例代码附效果图
- 32.Android端向web端(服务器)提交数据(GET-POST-AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- 异步http提交数据到服务器
- httpClient及android 原生接口实现下载并显示图片
- HttpClient的get和post方式提交数据的使用
- 从其他服务器上下载数据.通过webclient