博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中post和get请求
阅读量:7005 次
发布时间:2019-06-28

本文共 5845 字,大约阅读时间需要 19 分钟。

示例代码:

package com.shareboxes.util;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLConnection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.junit.Ignore;import org.junit.Test;/*** @ClassName: HttpRequest* @Description: get,post请求* @author Administrator* @date 2015年10月19日* */public class HttpRequest {    /**    * @Title: sendGet    * @Description: get请求    * @param url    * @param param     */    public static String sendGet(String url, Map
param) { BufferedReader bReader = null; StringBuffer sBuffer = new StringBuffer(); String realUrl = url; try { if (param.size() > 0) { realUrl += "?"; for (Entry
entry : param.entrySet()) { realUrl += entry.getKey() + "=" + entry.getValue() + "&"; } realUrl = realUrl.substring(0, realUrl.length() - 1); } URL urlString = new URL(realUrl); URLConnection urlConnection = urlString.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; httpURLConnection.setRequestMethod("GET");// 设置请求方法 httpURLConnection.setConnectTimeout(30000);// 连接主机超时时间 httpURLConnection.setReadTimeout(30000);// 读取数据超时时间 httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 设置请求数据的格式 httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");// 设置接收数据的编码 // 判断连接是否异常 if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } System.out.println(httpURLConnection.getResponseCode()); for(Entry
>entry:httpURLConnection.getHeaderFields().entrySet()){ System.out.println(entry.getKey()+"--------->"+entry.getValue()); } bReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8")); String line = null; while ((line = bReader.readLine()) != null) { sBuffer.append(line); } } catch (Exception e) { System.out.println("get 请求发生错误!!!"); e.printStackTrace(); } finally { if (bReader != null) { try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sBuffer.toString(); } /** * @Title: sendPost * @Description: post请求 * @param url * @param param */ public static String sendPost(String url, Map
param) { BufferedReader bReader = null; OutputStreamWriter out = null; StringBuffer sBuffer = new StringBuffer(); String parameterData =""; try { if (param.size() > 0) { for (Entry
entry : param.entrySet()) { parameterData += entry.getKey() + "=" + entry.getValue() + "&"; } parameterData = parameterData.substring(0, parameterData.length() - 1); } URL realUrl = new URL(url); HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection(); httpURLConnection.setConnectTimeout(30000); httpURLConnection.setReadTimeout(30000); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setInstanceFollowRedirects(true); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 设置请求参数的格式 httpURLConnection.setRequestProperty("Connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");// 接收数据的编码 if((parameterData.trim().length()>0) && (!parameterData.equals(""))){ out = new OutputStreamWriter(httpURLConnection.getOutputStream(), "utf-8"); out.write(parameterData); out.flush(); } // 判断连接是否异常 if (httpURLConnection.getResponseCode() >= 300) { throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode()); } System.out.println(httpURLConnection.getResponseCode()); for(Entry
>entry:httpURLConnection.getHeaderFields().entrySet()){ System.out.println(entry.getKey()+"--------->"+entry.getValue()); } // bReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));// String line = null;// while ((line = bReader.readLine()) != null) {// sBuffer.append(line);// } InputStream in=httpURLConnection.getInputStream(); byte []data=new byte[httpURLConnection.getContentLength()]; int offset=0; while(offset
param = new HashMap
(); param.put("tid", "1"); System.out.println(sendGet("http://localhost:8080/shareboxes/record/getrecord/tid.do", param)); } @Test public void testPost() { Map
param = new HashMap
(); param.put("tid", "1"); System.out.println(sendPost("http://localhost:8080/shareboxes/record/getrecord/tid.do", param)); }}

转载于:https://www.cnblogs.com/dmir/p/4892981.html

你可能感兴趣的文章
CMD命令集-Color
查看>>
android 消息推送 记录
查看>>
Java文件下载
查看>>
Mybatis #{} 与 ${} 区别
查看>>
java体系和微软体系
查看>>
js bind() 第一个参数为null
查看>>
dubbo序列化问题(一)浮点数问题
查看>>
git stash
查看>>
mysql 多个实例
查看>>
memcached 常用命令及使用说明
查看>>
UIScrollView详解
查看>>
从《微习惯》中得到的
查看>>
Ubuntu12.04下以tar.gz包方式安装Go语言
查看>>
软件版本说明-Alpha/Belta/RC/GA
查看>>
Django多进程日志文件问题
查看>>
Dispatcher initialization failed问题的解决方法
查看>>
尝试运行tagin见证Android 3D云标签的效果
查看>>
移植qt5.3.1到arm
查看>>
重温一遍rails中ajax的操作
查看>>
李刚:搜索引擎营销新思路
查看>>