Http进行网络通信

http使用get的方式进行网络通信:

package com.testGet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 使用Http的Get方式读取网络数据
 */
public class TestGet {
    public static void main(String[] args) {
        new ReadByGet("welcome").start();
    }
}
class ReadByGet extends Thread{
    private String str;
    public ReadByGet(String str){
        this.str=str;
    }
    @Override
    public void run() {
        try {
            
            
            
            URL url=new URL("http://fanyi.youdao.com/openapi.do?keyfrom=guodaxia&key=1142217390&type=data&doctype=xml&version=1.1&q="+str);
            URLConnection connection=url.openConnection();//打开链接
            InputStream is=connection.getInputStream();//获取输入流
            InputStreamReader isr=new InputStreamReader(is,"UTF-8");
            BufferedReader br=new BufferedReader(isr);
            
            String line;
            StringBuilder builder=new StringBuilder();
            while((line=br.readLine())!=null){
                builder.append(line);
            }
            br.close();
            isr.close();
            is.close();
            System.out.println(builder);
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
View Code

http使用post的方式进行网络通信:

package com.testPost;

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;
/**
 * 使用Http的Post方式与网络交互通信
 * @author Administrator
 *
 */
public class TestPost {
    public static void main(String[] args) {
        new ReadByPost().start();
    }
}
class ReadByPost extends Thread{
    @Override
    public void run() {
        try {
            
            URL url=new URL("http://fanyi.youdao.com/openapi.do");
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();//Post使用的是HttpURLConnection
            connection.addRequestProperty("encoding", "utf-8");
            //打开输入输出通道
            connection.setDoInput(true);
            connection.setDoOutput(true);
            
            OutputStream os=connection.getOutputStream();
            OutputStreamWriter osw=new OutputStreamWriter(os);
            BufferedWriter bw=new BufferedWriter(osw);
            
            bw.write("keyfrom=guodaxia&key=1142217390&type=data&doctype=xml&version=1.1&q=welcome");
            bw.flush();
            
            InputStream is=connection.getInputStream();
            InputStreamReader isr=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(isr);
            
            String line;
            StringBuilder builder=new StringBuilder();
            while((line=br.readLine())!=null){
                builder.append(line);
            }
            bw.close();
            osw.close();
            os.close();
            br.close();
            isr.close();
            is.close();
            
            System.out.println(builder);
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
View Code

httpClient使用get方式进行网络通信:

package com.testClientGet;

import java.io.IOException;

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;



/**
 * 使用HttpClient进行Get方式通信
 * @author Administrator
 *
 */
public class ClientGet {
    public static void main(String[] args) {
        new Get().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();
        }
    }
    
}
View Code

httpClient使用post方式进行网络通信:

package com.testClientPost;

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.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 使用HttpClient进行Post方式通信
 * @author Administrator
 *
 */
public class TestPost {
    public static void main(String[] args) {
        //http://fanyi.youdao.com/openapi.do
        //keyfrom=guodaxia&key=1142217390&type=data&doctype=xml&version=1.1&q=welcome
        new Post().start();
    }
}
class Post extends Thread{
    HttpClient client=HttpClients.createDefault();
    
    @Override
    public void run() {
        HttpPost post=new HttpPost("http://fanyi.youdao.com/openapi.do");
        try {
            //Post需要请求参数
            //keyfrom=guodaxia&key=1142217390&type=data&doctype=xml&version=1.1&q=welcome
            List<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>();
            parameters.add(new BasicNameValuePair("keyfrom", "guodaxia"));
            parameters.add(new BasicNameValuePair("key", "1142217390"));
            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,"UTF-8"));
            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();
        }
    }
}
View Code

注意:

我这里使用的httpClient是4.3.5版本的

有道翻译api信息在我的邮箱中

posted @ 2016-07-13 14:45  guodaxia  阅读(330)  评论(0编辑  收藏  举报