Java中HTTP通訊

來源:互聯網
上載者:User

標籤:

  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通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.