码迷,mamicode.com
首页 > 编程语言 > 详细

Java中HTTP通信

时间:2015-07-20 01:02:38      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

  Java自带的get、post请求:

  get请求方式:

package com.java;

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;
/**
 * java自带get请求方式
 * @author yxm
 *
 */
public class HttpGet {
    public static void main(String[] args) {
        new Get().start();
    }
    static class Get extends Thread{
        @Override
        public void run() {
                try {
                    URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=huhailang&key=557910319&type=data&doctype=xml&version=1.1&q=welcome");
                    URLConnection connection = url.openConnection();
                    InputStream is = connection.getInputStream();
                    InputStreamReader isr = new InputStreamReader(is);
                    BufferedReader br = new BufferedReader(isr);
                    
                    String line;
                    StringBuffer builder = new StringBuffer();
                    while((line=br.readLine())!=null){
                        builder.append(line);
                    }
                    br.close();
                    isr.close();
                    is.close();
                    System.out.println(builder.toString());
                    
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    // TODO: handle exception
                }
        }
    }
}

  Java自带的post请求:

package com.java;
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;
/**
 * Java自带的post请求
 * @author yxm
 *
 */
public class HttpPost {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Post().start();
    }
    static class Post extends Thread{
        @Override
        public void run() {
                try {
                    //http://fanyi.youdao.com/openapi.do?keyfrom=huhailang&key=557910319&type=data&doctype=xml&version=1.1&q=welcome
                    URL url = new URL("http://fanyi.youdao.com/openapi.do");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.addRequestProperty("encoding", "UTF-8");
                    connection.setDoOutput(true);
                    connection.setDoInput(true);
                    connection.setRequestMethod("POST");
                    
                    OutputStream os = connection.getOutputStream();
                    OutputStreamWriter osw = new OutputStreamWriter(os);
                    BufferedWriter bw = new BufferedWriter(osw);
                    bw.write("keyfrom=huhailang&key=557910319&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.toString());
                    
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }catch (IOException e) {
                    // TODO: handle exception
                }
        }
    }

}

  Apache提供的HTTPclient,同样也包含get和post两种方式

  get方式:

public class HttpGets {
    public static void main(String[] args) {
        new Get().start();
    }
    
    static class Get extends Thread{
        HttpClient client = HttpClients.createDefault();
        @Override
        public void run() {
                HttpGet get = new HttpGet("www.baidu.com");
                
                HttpResponse reponse = client.excute(get);
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString("encoding","UTF-8");
                
                System.out.println(result);            
        }
    }
}

  post方式:

public class HttpPosts {

    public static void main(String[] args) {
        new Post().start();
    }
    static class Post extends Thread{
        HttpClient client = HttpClients.createDefault();
        @Override
        public void run() {
                HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do");
                
                List<BasicNameValuePair> paramters = new ArrayList<>();
                paramters.add(new BasicNameValuePair("keyform","huhailang"));
                paramters.add(new BasicNameValuePair("key","557910319"));
                paramters.add(new BasicNameValuePair("type","data"));
                paramters.add(new BasicNameValuePair("doctype","xml"));
                paramters.add(new BasicNameValuePair("version","1.1"));
                paramters.add(new BasicNameValuePair("q","welcome"));
                
                post.setEntity(new UrlEncodedFormEntity(paramters,"UTF-8"));
                HttpResponse reponse = client.excute(post);
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString("encoding","UTF-8");
                
                System.out.println(result);            
        }
    }

}

Java中HTTP通信

标签:

原文地址:http://www.cnblogs.com/tomcatx/p/4660181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!