• Java中HTTP通信


      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两种方式

      httpclient下载地址:http://hc.apache.org/downloads.cgi

      

      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);            
            }
        }
    
    }
  • 相关阅读:
    标题栏中小图标和文字垂直居中的解决办法
    width:100%和width:inherit
    css盒子模型的宽度问题
    position:absolute和margin:auto 连用实现元素水平垂直居中
    超链接a的download属性 实现文件下载功能
    JavaScript的String对象的属性和方法
    原生JavaScript 封装ajax
    深入理解JVM之对象分配流程
    http协议详解
    在RMI方式实现RPC时,为什么业务实现类UserServiceImpl中要显示的创建无参构造方法
  • 原文地址:https://www.cnblogs.com/tomcatx/p/4660181.html
Copyright © 2020-2023  润新知