HttpClientGet与HttpClientPost

本文有待修改
HttpClientGet

package com.http;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
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.HttpClientBuilder;

public class HttpClientdoget extends JFrame {

    private JPanel contentPane;

    /** * Launch the application. */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HttpClientdoget frame = new HttpClientdoget();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** * Create the frame. */
    public HttpClientdoget() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                String urlstring="http://localhost:8080/MyServiceTest/Myserverlettest?username=zhangsan&password=123456";
                HttpClientBuilder builder=HttpClientBuilder.create();//HttpClientBuilder的创建
                HttpClient client=builder.build();//获得client
                builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS);

                HttpGet get=new HttpGet(urlstring);//获得get,建立连接
                get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");//设置编码
                HttpResponse response;
                try {
                    response = client.execute(get);
                    StatusLine statusline=response.getStatusLine();
                    int code=response.hashCode();
                    if(code==HttpURLConnection.HTTP_OK){
                        HttpEntity entity=response.getEntity();
                        InputStream is=entity.getContent();
                        BufferedReader br=new BufferedReader(new InputStreamReader(is));
                        String line=br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line=br.readLine();
                        }
                }
                    }catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }           
            }
        });
        btnNewButton.setBounds(184, 90, 93, 69);
        contentPane.add(btnNewButton);
    }

}

HttpClientPost

package com.http;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import com.sun.jna.platform.win32.WinUser.INPUT;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class HttpClientdopost extends JFrame {

    private JPanel contentPane;

    /** * Launch the application. */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    HttpClientdopost frame = new HttpClientdopost();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** * Create the frame. */
    public HttpClientdopost() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String url="http://localhost:8080/MyServiceTest/Myserverlettest";
                HttpClientBuilder builder=HttpClientBuilder.create();

                HttpClient client=builder.build();
                HttpPost post=new HttpPost(url);

                NameValuePair pair1=new BasicNameValuePair("username","张三");
                NameValuePair pair2=new BasicNameValuePair("password","123456");
                ArrayList<NameValuePair>  params=new ArrayList<>();
                params.add(pair1);
                params.add(pair2);
                try {
                    post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
                    post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    HttpResponse respose=client.execute(post);
                    int code=respose.getStatusLine().getStatusCode();
                    if(code==200){
                        HttpEntity entity=respose.getEntity();
                        InputStream is=entity.getContent();
                        BufferedReader br=new BufferedReader(new InputStreamReader(is));
                        String line=br.readLine();
                        while(line!=null){
                            System.out.println(line);
                            line=br.readLine();

                        }
                    }
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }






            }
        });
        btnNewButton.setBounds(118, 58, 154, 135);
        contentPane.add(btnNewButton);
    }

}

你可能感兴趣的:(HttpClientGet与HttpClientPost)