java关于post请求与get请求

来源:互联网 发布:英语学习软件开发 编辑:程序博客网 时间:2024/05/02 04:42

代码如下:


import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;public class PostAndGetTest {public final static String postUrl="http://localhost:8080/VMPost/SendPost";public final static String getUrl="http://172.17.254.228:8088/monitor/monitorData/listMonitorDatas";/** * 发送post请求 * url 请求路径 * 参数在方法内封装,改写成工具类的话,直接传参也可 */public String sendPost(String url){String result="";//设置参数List<NameValuePair> params = new ArrayList<NameValuePair>();params.add(new BasicNameValuePair("resourceUniqueKey", "1"));params.add(new BasicNameValuePair("key", "1"));params.add(new BasicNameValuePair("scope", "60"));params.add(new BasicNameValuePair("startTime", "Fri Oct 21 2016 12:38:12 GMT+0800 (中国标准时间)"));params.add(new BasicNameValuePair("endTime", "Fri Oct 21 2016 15:38:12 GMT+0800 (中国标准时间)"));// 创建HttpPost对象HttpPost httpRequst = new HttpPost(url);try {// post请求参数写入httprequest对象httpRequst.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));// 发送请求HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequst);// http响应代码200为成功Integer statusCode=httpResponse.getStatusLine().getStatusCode();if (statusCode == 200) {// 取出应答字符串HttpEntity httpEntity = httpResponse.getEntity();result = EntityUtils.toString(httpEntity);}else{return statusCode.toString();}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}/** * 发送get请求 * url 请求路径 * 参数在方法内封装,改写成工具类的话,直接传参也可 */public String sendGet(String url){String result = "";        BufferedReader in = null;        //get请求使用url传参        String params="?resourceUniqueKey=1&formula=avg&key=system.cpu.util[,user]&scope=60&startTime=1476936000&endTime=1476946800";try {URL realUrl = new URL(url+params);// 打开和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<String, List<String>> map = connection.getHeaderFields();    // 遍历所有的响应头字段for (String k : map.keySet()) {    System.out.println(k + "--->" + map.get(k));}// 定义 BufferedReader输入流来读取URL的响应in = new BufferedReader(new InputStreamReader(        connection.getInputStream()));String line;while ((line = in.readLine()) != null) {    result += line;}} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}public static void main(String[] args) {PostAndGetTest test=new PostAndGetTest();String postResult=test.sendPost(postUrl);String getResult=test.sendGet(getUrl);System.out.println("post请求返回:");System.out.println(postResult);System.out.println("get请求返回:");System.out.println(getResult);}}


源码以及jar包下载

0 0
原创粉丝点击