1.使用Http的get方式通信
package com.example; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; public class MyClass { public static void main(String []args){ new tetsGet("http://fanyi.youdao.com/openapi.do?keyfrom=TESTINTERNETssss&key=1185096979&type=data&doctype=json&version=1.1&q=hello").start(); } } class tetsGet extends Thread{ private String string = new String(); public tetsGet(String url) { this.string = url; } @Override public void run() { try { URL url = new URL(this.string); URLConnection urlConnection = url.openConnection(); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder str = new StringBuilder(); String line = new String(); while ((line = br.readLine())!=null){ str.append(line); } br.close(); isr.close(); is.close(); System.out.println(str); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
2.使用Http的post方式通信
package com.example; 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 MyClass { public static void main(String []args){ new testPost().start(); } } class testPost extends Thread{ @Override public void run() { try { URL url = new URL("http://fanyi.youdao.com/openapi.do"); HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection(); urlConnection.addRequestProperty("encoding","UTF-8"); urlConnection.setDoOutput(true); urlConnection.setDoInput(true); urlConnection.setRequestMethod("POST"); OutputStream os = urlConnection.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os); BufferedWriter bw = new BufferedWriter(osw); bw.write("keyfrom=TESTINTERNETssss&key=1185096979&type=data&doctype=json&version=1.1&q=hello"); bw.flush(); InputStream is = urlConnection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String str = new String(); while ((str = br.readLine())!= null){ sb.append(str); } System.out.println(sb); br.close(); isr.close(); is.close(); bw.close(); osw.close(); os.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3.使用HttpClient的get方式通信
package com.example; 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.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; public class MyClass { public static void main(String []args){ new testget().start(); } } class testget 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(); } } }
4.使用HttpcClient的post方式通信
package com.example; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; 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; import org.w3c.dom.Entity; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class MyClass { public static void main(String []args){ new testpost().start(); } } //keyfrom=TESTINTERNETssss&key=1185096979&type=data&doctype=json&version=1.1&q=hello class testpost extends Thread{ HttpClient client = HttpClients.createDefault(); @Override public void run() { HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do"); try { List<BasicNameValuePair> para = new ArrayList<>(); para.add(new BasicNameValuePair("keyfrom","TESTINTERNETssss")); para.add(new BasicNameValuePair("key","1185096979")); para.add(new BasicNameValuePair("type","data")); para.add(new BasicNameValuePair("doctype","json")); para.add(new BasicNameValuePair("version","1.1")); para.add(new BasicNameValuePair("q","hello")); post.setEntity(new UrlEncodedFormEntity(para,"UTF-8")); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity(); String str = EntityUtils.toString(entity,"UTF-8"); System.out.println(str); } catch (IOException e) { e.printStackTrace(); } } }