HttpClientGet与HttpClientPost
来源:互联网 发布:淘宝开店考试答案2016 编辑:程序博客网 时间:2024/06/07 19:51
本文有待修改
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); }}
0 0
- HttpClientGet与HttpClientPost
- HttpClientPost
- HttpClientPOST
- HttpClientGet
- HttpClientGET
- httpclientpost网络获取数据
- httpclientget网络请求数据
- 关于HttpClientPost的具体用法
- 关于HttpClientGet的具体用法
- 通过 httpclientget 方法 向服务器中请求数据
- 一个简单地应用--从客户端向服务器发送数据.post,get.httpclientget,httpclinetpost,文件上传.
- 与
- 与
- “” 与 “”
- ##与#
- >> 与 > >
- :与::
- &与&&
- internal 修饰符起什么作用?
- 不确定的问题 2015-8-12
- android控件--ViewPager原理及实现
- ViewDragHelper详解
- easy_install安装,更新模块 python
- HttpClientGet与HttpClientPost
- iPhone 6 / 6 Plus 设计·适配方案
- Cacti的安装与插件开发(一)
- swift - The Composite Pattern
- SQLite基础学习
- 转载:Ant学习笔记——自己构建Ant编译环境
- 思维导图-理清发展思路
- DCIM 用 UIScrollView 和UIPageControl
- Tire学习总结