博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Http(get,post)及HttpClient(get,post)的简单使用
阅读量:5818 次
发布时间:2019-06-18

本文共 7068 字,大约阅读时间需要 23 分钟。

 

  

 

1. 使用 Http 的 Get 方式读取网络数据

  

1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStream; 4 import java.io.InputStreamReader; 5 import java.net.MalformedURLException; 6 import java.net.URL; 7 import java.net.URLConnection; 8  9 public class TestHttpGet {10 11     public static void main(String[] args) {12         new ReadByGet().start();13     }14     static class ReadByGet extends Thread{15         @Override16         public void run() {17             try {18                 URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=jkxyuvi&key=1530995034&type=data&doctype=xml&version=1.1&q=welcom");19                 URLConnection connection = url.openConnection();20                 InputStream is = connection.getInputStream();21                 InputStreamReader isr = new InputStreamReader(is);22                 BufferedReader br = new BufferedReader(isr);23                 24                 String line;25                 StringBuilder builder = new StringBuilder();26                 27                 while((line = br.readLine())!=null){28                     builder.append(line);29                     30                 }31                 br.close();32                 isr.close();33                 is.close();34                 35                 System.out.println(builder.toString());36                 37             } catch (MalformedURLException e) {38                 39                 e.printStackTrace();40             } catch (IOException e) {41                 e.printStackTrace();42             }43         }44     }45 46 }

 

2. 使用 Http 的 Post 方式与网络交互通信

1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.InputStreamReader; 6 import java.io.OutputStream; 7 import java.io.OutputStreamWriter; 8 import java.net.HttpURLConnection; 9 import java.net.MalformedURLException;10 import java.net.URL;11 12 public class TestHttpPost {13 14     public static void main(String[] args) {15         new ReadByPost().start();16     }17 18 }19 class ReadByPost extends Thread{20     @Override21     public void run() {22         try {23             URL url = new URL("http://fanyi.youdao.com/openapi.do");24             HttpURLConnection connection = (HttpURLConnection) url.openConnection();25             connection.addRequestProperty("encoding", "UTF-8");26             connection.setDoInput(true);27             connection.setDoOutput(true);28             connection.setRequestMethod("POST");29             30             31             32             OutputStream os = connection.getOutputStream();33             OutputStreamWriter osw = new OutputStreamWriter(os);34             BufferedWriter bw = new BufferedWriter(osw);35             36             37             38             bw.write("keyfrom=jkxyuvi&key=1530995034&type=data&doctype=xml&version=1.1&q=welcome");39             bw.flush();//强制输出40             41             InputStream is = connection.getInputStream();42             InputStreamReader isr = new InputStreamReader(is);43             BufferedReader br = new BufferedReader(isr);44             String line;45             StringBuilder builder = new StringBuilder();46             while((line = br.readLine())!=null){47                 builder.append(line);48             }49             50             bw.close();51             osw.close();52             os.close();53             br.close();54             isr.close();55             is.close();56             System.out.println(builder.toString());57             58         } catch (MalformedURLException e) {59             // TODO Auto-generated catch block60             e.printStackTrace();61         } catch (IOException e) {62             // TODO Auto-generated catch block63             e.printStackTrace();64         }65     }66 }

 

3. 使用HttpClient进行Get方式通信

先下载HttpClient相关依赖包,地址:

1 import java.io.IOException; 2  3 import org.apache.http.HttpEntity; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.client.ClientProtocolException; 6 import org.apache.http.client.HttpClient; 7 import org.apache.http.client.methods.HttpGet; 8 import org.apache.http.impl.client.HttpClients; 9 import org.apache.http.util.EntityUtils;10 11 public class TestHttpClientGet {12 13     public static void main(String[] args) {14         new Get().start();15     }16 17 }18 class Get extends Thread{19     20     HttpClient client = HttpClients.createDefault();21     22     23     @Override24     public void run() {25         26         HttpGet get = new HttpGet("http://www.bai.com");27         try {28             HttpResponse response = client.execute(get);29             HttpEntity entity = response.getEntity();30             String result  =  EntityUtils.toString(entity,"UTF-8");31             System.out.println(result);32         } catch (ClientProtocolException e) {33             e.printStackTrace();34         } catch (IOException e) {35             e.printStackTrace();36         }37         38     }39 }

 

4. 使用 HttpClient 进行 Post 方式通信

1 import java.io.IOException; 2 import java.util.ArrayList; 3 import java.util.List; 4  5 import org.apache.http.HttpEntity; 6 import org.apache.http.HttpResponse; 7 import org.apache.http.client.ClientProtocolException; 8 import org.apache.http.client.HttpClient; 9 import org.apache.http.client.entity.UrlEncodedFormEntity;10 import org.apache.http.client.methods.HttpPost;11 import org.apache.http.impl.client.HttpClients;12 import org.apache.http.message.BasicNameValuePair;13 import org.apache.http.util.EntityUtils;14 15 public class TestHttpClientPost {16 17     public static void main(String[] args) {18         new Post().start();19     }20 21 }22 class Post extends Thread{23     24     HttpClient client = HttpClients.createDefault();25     26     @Override27     public void run() {28         HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do");29         30         try {31             List
parameters = new ArrayList<>();32 parameters.add(new BasicNameValuePair("keyfrom", "jkxyuvi"));33 parameters.add(new BasicNameValuePair("key", "1530995034"));34 parameters.add(new BasicNameValuePair("type", "data"));35 parameters.add(new BasicNameValuePair("doctype", "xml"));36 parameters.add(new BasicNameValuePair("version", "1.1"));37 parameters.add(new BasicNameValuePair("q", "welcome"));38 post.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8"));39 40 HttpResponse response = client.execute(post);41 HttpEntity entity = response.getEntity();42 String result = EntityUtils.toString(entity,"UTF-8");43 System.out.println(result);44 45 } catch (ClientProtocolException e) { 46 e.printStackTrace();47 } catch (IOException e) {48 e.printStackTrace();49 }50 51 52 53 }54 }

 

与java自带的http通信,HttpClient用起来确实方便的多。

转载地址:http://oqwdx.baihongyu.com/

你可能感兴趣的文章
Spring Cloud 微服务分布式链路跟踪 Sleuth 与 Zipkin
查看>>
ORM数据库框架 SQLite 常用数据库框架比较 MD
查看>>
华为OJ 名字美丽度
查看>>
微信公众号与APP微信第三方登录账号打通
查看>>
onchange()事件的应用
查看>>
Windows 下最佳的 C++ 开发的 IDE 是什么?
查看>>
软件工程师成长为架构师必备的十项技能
查看>>
python 异常
查看>>
百度账号注销
查看>>
mysql-This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME 错误解决
查看>>
BIEE Demo(RPD创建 + 分析 +仪表盘 )
查看>>
Cocos2dx 3.0开发环境的搭建--Eclipse建立在Android工程
查看>>
基本概念复习
查看>>
重构第10天:提取方法(Extract Method)
查看>>
Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理
查看>>
解决pycharm在ubuntu下搜狗输入法一直固定在左下角的问题
查看>>
“Info.plist” couldn’t be removed
查看>>
多线程day01
查看>>
react-native 模仿原生 实现下拉刷新/上拉加载更多(RefreshListView)
查看>>
MySQL出现Access denied for user ‘root’@’localhost’ (using password:YES)
查看>>