您的位置:首页 > 理论基础 > 计算机网络

Http POST与GET请求JAVA实现

2015-03-13 23:38 447 查看
实现HTTP POST与GET请求,最近使用该工具类实现了调用websrvice接口.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Map.Entry;

/***
* <p>
* Title: Http 客服端工具类
* </p>
* <p>
* Description:
* </p>
* @author:落叶飞
* @version 1.0
* @history: Created by 落叶飞 2015-3-13
* @webio:<a href="http://weibo.com/lyfxinsui">七里外风车磨坊</a>
*/
public class HttpClientUtil {
/***
* <p>
* Description:发送HTTP POST请求
* <p>
*
* @param serverUrl
*            :服务端地址
* @param sendParms
*            :发送post请求参数,map中key为参数名,value为参数值
* @return
* @author 陈波 2015-3-11
*/
public final static String sendPostRequest(String serverUrl, Map<String, String> sendParms) {
return sendRequest(serverUrl, sendParms, "POST");
}

/***
* <p>
* Description:发送HTTP GET请求
* <p>
*
* @param serverUrl
*            :服务端地地址
* @param sendParms
*            :发送get请求参数,map中key为参数名,value为参数值
* @return
* @author 陈波 2015-3-11
*/
public final static String sendGetRequest(String serverUrl, Map<String, String> sendParms) {
return sendRequest(serverUrl, sendParms, "GET");
}

/***
*
* <p>
* Description:发送HTTP请求 POST 或者GET
* <p>
*
* @param serverUrl
*            :请求服务地址
* @param sendParms
*            :请求参数和值
* @param sendMethod
*            :发请求类型 POST或者GET
* @return
* @author 陈波 2015-3-13
*/
public static final String sendRequest(String serverUrl, Map<String, String> sendParms, String sendMethod) {

if (null != sendMethod && !"".equals(sendMethod) && ("post".equalsIgnoreCase(sendMethod) || "get".equalsIgnoreCase(sendMethod))) {

StringBuffer resultBuffer = new
a71b
StringBuffer();// 请求结果
StringBuffer sendParmsBuffer = new StringBuffer();
URL url = null;
HttpURLConnection httpURLConnection = null; // http 连接对象
OutputStreamWriter outputStreamWriter = null; // post 发送数据
BufferedReader bufferedReader = null; // 按照行结果获取结果数据

if (null != sendParms && 0 < sendParms.size()) {
// 构造HTTP 请求参数格式
int index = sendParms.size() - 1;
for (Entry<String, String> objParms : sendParms.entrySet()) {
sendParmsBuffer.append(objParms.getKey() + "=" + objParms.getValue());
if (index != 0) {
sendParmsBuffer.append("&");
}
index--;
}
if (sendMethod.toUpperCase().equals("GET")) {
serverUrl = serverUrl + "?";
serverUrl += sendParmsBuffer.toString();
}
}
try {
url = new URL(serverUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod(sendMethod.toUpperCase());
httpURLConnection.setUseCaches(false);
httpURLConnection.setInstanceFollowRedirects(false);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.connect();
if (0 < sendParmsBuffer.length() && sendMethod.toUpperCase().equals("POST")) {
outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8");
outputStreamWriter.write(sendParmsBuffer.toString());
outputStreamWriter.flush();
}
bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
String line;
while (null != (line = bufferedReader.readLine())) {
resultBuffer.append(line);
}
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("请求失败:" + e.getMessage());
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("请求失败:" + e.getMessage());
} finally {
if (null != httpURLConnection) {
httpURLConnection.disconnect();
}
if (null != outputStreamWriter) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != bufferedReader) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
return resultBuffer.toString();

}
return null;

}

}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java HTTP POST GET 请求