HttpClient/ HttpUrlConnection/UrlConnection
2015-08-12 10:01
741 查看
常用编码转换
deGet方法测试
DoPost
HttpClientDoGet
HttpClientDoPOST
HttpUrlConnection:sun公司封装的网络连接
doGet
doPost
HttpClient:Apache使用HttpURLConnection封装的类
HttpClientdoGet
HttpClientdoPost
常用编码转换
// 设置接受的数据类型 httpConnection.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
deGet方法测试
JButton btnNewButton = new JButton("doGet方法测试"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456"; try { URL url=new URL(urlString);//生成URL URLConnection connect=url.openConnection();//打开url链接 //强制转型成httpurlconnection HttpURLConnection httpConnection=(HttpURLConnection)connect; //设置请求方法 httpConnection.setRequestMethod("GET"); //设置编码格式 // 设置接受的数据类型 httpConnection.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); int code=httpConnection.getResponseCode(); System.out.println("状态码:"+code); if(code==HttpURLConnection.HTTP_OK){ InputStream is=httpConnection.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); while (line!=null){ System.out.println(line); line=br.readLine(); } }
#
1生成URL ——->URL url=new URL(urlString);2开url链接——–>URLConnection connect=url.openConnection();
3强制转型httpurlconnection——->HttpURLConnection httpConnection=(HttpURLConnection)connect;
DoPost
//设置请求方法httpConnection.setRequestMethod(“POST”);
//设置可以读取服务器返回的内容默认true
httpConnection.setDoInput(true);
//设置哭护短可以给服务器提交数据,默认为false但POST必须设置true
httpConnection.setDoOutput(true);
//POST方法不允许使用缓存
httpConnection.setUseCaches(false);
String params=”username=zhangsan&password=lisi”;
httpConnection.getOutputStream().write(params.getBytes());
public void actionPerformed(ActionEvent arg0) { String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456"; try { URL url=new URL(urlString);//生成URL URLConnection connect=url.openConnection();//打开url链接 //强制转型成httpurlconnection HttpURLConnection httpConnection=(HttpURLConnection)connect; //设置读取时间超时 httpConnection.setReadTimeout(30000); //设置链接超时时间 httpConnection.setConnectTimeout(30000); //设置编码格式 // 设置接受的数据类型 httpConnection.setRequestProperty("Accept-Charset", "utf-8"); // 设置可以接受序列化的java对象 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置请求方法 httpConnection.setRequestMethod("POST"); //设置可以读取服务器返回的内容默认true httpConnection.setDoInput(true); //设置哭护短可以给服务器提交数据,默认为false但POST必须设置true httpConnection.setDoOutput(true); //POST方法不允许使用缓存 httpConnection.setUseCaches(false); String params="username=zhangsan&password=lisi"; httpConnection.getOutputStream().write(params.getBytes()); int code=httpConnection.getResponseCode(); System.out.println("状态码:"+code); if(code==HttpURLConnection.HTTP_OK){ InputStream is=httpConnection.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String line=br.readLine(); while (line!=null){ System.out.println(line); line=br.readLine(); } }2 } catch (MalformedURLException e) { e.printStackTrace(); } catch (ConnectException e) { System.out.println("服务器拒绝链接"); e.printStackTrace(); }catch ( SocketTimeoutException e) { System.out.println("网络连接超时"); e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
HttpClientDoGet
JButton btnNewButton = new JButton("HttpClientDoGet"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String urlString="http://localhost:8080/MyServiceTest/MyTestServerlet?username=zhangsan&password=123456"; HttpClientBuilder builder=HttpClientBuilder.create(); builder.setConnectionTimeToLive(3000, TimeUnit.MILLISECONDS); //生成client的buidler HttpClient client=builder.build();//生成client HttpGet get=new HttpGet(urlString);//设置为get方法 get.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //设置服务器接收后数据的读取方式为utf8 try { HttpResponse response=client.execute(get);//执行get方法得到服务器的返回的所有数据都在response中 StatusLine statusLine=response.getStatusLine();//httpClient访问服务器返回的表头,包含http状态码 int code=statusLine.getStatusCode();//得到状态码 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 e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } });
HttpClientDoPOST
public void actionPerformed(ActionEvent arg0) { String urlString = "http://localhost:8080/MyServiceTest/MyTestServerlet"; HttpClientBuilder builder = HttpClientBuilder.create(); builder.setConnectionTimeToLive(3000, TimeUnit.MICROSECONDS); // 生成client的builder HttpClient client = builder.build(); HttpPost post = new HttpPost(urlString);// 设置为get方法 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 response =client.execute(post); int code =response.getStatusLine().getStatusCode(); if(code==200){ 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(); } }
相关文章推荐
- SM32W108无线射频模块多个节点之间通信实例
- HTTP Debugger Pro v6.4 + Keygen For HTTP Debugger Pro v6.x
- 使用charles proxy for Mac来抓取手机App的网络包
- centos 设置网络 /etc/sysconfig/network,/etc/sysconfig/network-scripts/ifcfg-eth0
- 使用NSURLConnection发送http网络请求
- 真有超级IP?亿万级泡沫谁来接盘
- HttpUrlConnection和HttpClient的使用(doGet( )、doPost( )方法)
- 打造安全的App!iOS安全系列之 HTTPS
- 打造安全的App!iOS安全系列之 HTTPS
- 无线网络
- C++ 简单实现HTTP GET/POST 请求
- HTTP协议浅析 (2013-08-12 19:48:14) http://blog.sina.com.cn/s/blog_987d7bdd0101nxtu.html
- HDU 4279 Number(2012天津网络游戏---数论分析题)
- C#网络编程(接收文件) - Part.5
- C#网络编程(订立协议和发送文件) - Part.4
- C#网络编程(同步传输字符串) - part.2
- 学习indy组件之一idhttp的使用方法
- [delphi]indy idhttp post方法
- Qt 网络开发基础
- Windows 2008 R2 2012 R2服务器备份高级应用之---增量备份到网络