本文共 4437 字,大约阅读时间需要 14 分钟。
客户端和服务器端的交互是使用get请求或post请求,尤其是移动端接口的请求,使用很多,经常是用了以后就不管了,下次使用还要去找,比较浪费时间,今天把这个get请求和post请求的写成博客,记录一下,方便大家参考使用。
一、get请求代码:
/** * @2015年11月19日上午9:27:26 * @function:向指定URL发送GET方法的请求 * @param url 发送请求的URL * @return result响应结果 * @authod :红桃峰峰 */ public static String get(String url) { String result = ""; BufferedReader in = null; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立实际的连接 connection.connect(); // 获取所有响应头字段 Map> map = connection.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println("返回结果的键值对是:"+key + "--->" + map.get(key)); } // 定义 BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader( connection.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送GET请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输入流 finally { try { if (in != null) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; }
二、post请求代码:
/** * @2015年11月19日上午9:30:56 * @function:向指定 URL 发送POST方法的请求 * @param url 发送请求的 URL * @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @param isproxy 是否使用代理模式:true使用,false不使用 * @return result响应结果 * @authod :红桃峰峰 */ public static String post(String url, String param, boolean isproxy) { OutputStreamWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); HttpURLConnection conn = null; if (isproxy) {// 使用代理模式 @SuppressWarnings("static-access") Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort)); conn = (HttpURLConnection) realUrl.openConnection(proxy); } else { conn = (HttpURLConnection) realUrl.openConnection(); } // 打开和URL之间的连接 // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod("POST"); // POST方法 // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.connect(); // 获取URLConnection对象对应的输出流 out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); // 发送请求参数 out.write(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!" + e); e.printStackTrace(); } // 使用finally块来关闭输出流、输入流 finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; }三、测试方法:
package com.http.util;public class TestHttpClient { public static void main(String[] args) { String url = "https://www.baidu.com/"; String result = HttpClientUtil.get(url); }}四、返回结果:
返回结果的键值对是:null--->[HTTP/1.1 200 OK]返回结果的键值对是:Cache-control--->[no-cache]返回结果的键值对是:Content-Length--->[227]返回结果的键值对是:X-UA-Compatible--->[IE=Edge,chrome=1]返回结果的键值对是:Last-Modified--->[Thu, 09 Oct 2014 10:47:57 GMT]返回结果的键值对是:Set-Cookie--->[__bsi=11932140861052763316_00_24_N_N_3_0301_002F_N_N_N_0; expires=Thu, 19-Nov-15 01:50:07 GMT; domain=www.baidu.com; path=/, BDSVRTM=0; path=/, PSTM=1447897802; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=0665B762DD3A1726BB0015DE95DEAB19; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BD_NOT_HTTPS=1; path=/; Max-Age=300]返回结果的键值对是:Connection--->[keep-alive]返回结果的键值对是:Server--->[bfe/1.0.8.9]返回结果的键值对是:Pragma--->[no-cache]返回结果的键值对是:Date--->[Thu, 19 Nov 2015 01:50:02 GMT]返回结果的键值对是:BDQID--->[0xf8efb6f400034aa3]返回结果的键值对是:P3P--->[CP=" OTI DSP COR IVA OUR IND COM "]返回结果的键值对是:BDPAGETYPE--->[1]返回结果的键值对是:Accept-Ranges--->[bytes]返回结果的键值对是:BDUSERID--->[0]返回结果的键值对是:Content-Type--->[text/html]
五、demo下载
转载地址:http://ormgi.baihongyu.com/