一、JDK自带的http通信机制--java.net.URL
package com.wjy;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class TestURL { public static void main(String[] args) { new GetRun().start();//get请求 new PostRun().start();//post请求 }}class GetRun extends Thread{ public void run() { try { //doctype=xml/json/jsonp URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=wjy-test&key=36384249&type=data&doctype=jsonp&callback=show&version=1.1&q=hello"); URLConnection connection = url.openConnection(); InputStream in = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(in,"utf-8"); BufferedReader br = new BufferedReader(isr); String line; StringBuilder sb = new StringBuilder(); while((line = br.readLine()) != null) { sb.append(line); } br.close(); isr.close(); in.close(); System.out.println(sb.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}class PostRun extends Thread{ @Override public void run() { try { URL url = new URL("http://fanyi.youdao.com/openapi.do"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("encoding", "UTF-8");//添加请求属性 connection.setDoInput(true);//允许输入 connection.setDoOutput(true);//允许输出 connection.setRequestMethod("POST");//POST请求 要在获取输入输出流之前设置 否则报错 //输出 OutputStream os = connection.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); bw.write("keyfrom=wjy-test&key=36384249&type=data&doctype=xml&version=1.1&q=welcome"); bw.flush(); //输入 InputStream in = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(in,"UTF-8"); BufferedReader br = new BufferedReader(isr); String line; StringBuilder sb = new StringBuilder(); while((line = br.readLine()) != null) { sb.append(line); } bw.close(); osw.close(); os.close(); br.close(); isr.close(); in.close(); System.out.println(sb.toString()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
二、apache中HttpComponents项目HttpClient工具
package com.wjy;import java.io.IOException;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class TestHttpclient { /** * 从apache官网下载HttpComponents项目jar包 */ public static void main(String[] args) { //new Get().start(); new Post().start(); }}class Get extends Thread{ HttpClient client = HttpClients.createDefault(); @Override public void run() { HttpGet get = new HttpGet("http://www.baidu.com"); try { HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); System.out.println(result); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}class Post extends Thread{HttpClient client = HttpClients.createDefault(); @Override public void run() { HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do"); try { Listparameters = new ArrayList (); parameters.add(new BasicNameValuePair("keyfrom", "wjy-test")); parameters.add(new BasicNameValuePair("key", "36384249")); parameters.add(new BasicNameValuePair("type", "data")); parameters.add(new BasicNameValuePair("doctype", "xml")); parameters.add(new BasicNameValuePair("version", "1.1")); parameters.add(new BasicNameValuePair("q", "welcome")); post.setEntity(new UrlEncodedFormEntity(parameters)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); System.out.println(result); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}