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 Listparameters = 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用起来确实方便的多。