您的位置:首页 > 编程语言 > Java开发

Java实现get或post请求

2017-12-11 21:21 288 查看
添加 jar 包支持

httpclient-4.3.1.jar
httpcore-4.3.jar
commons-logging-1.0.4.jar


编写工具类

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpUtils {

private static final String UTF_8 = "UTF-8";

public static String post(String url, Map<String, Object> params)
throws Exception {
String result = "";
HttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);

if (params != null && !params.isEmpty()) {
List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
for (Map.Entry<String, Object> entry : params.entrySet()) {
String name = entry.getKey();
String value = entry.getValue().toString();
BasicNameValuePair pair = new BasicNameValuePair(name, value);
parameters.add(pair);
}
httpPost.setEntity(new UrlEncodedFormEntity(parameters, UTF_8));
}
try {
HttpResponse response = client.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), UTF_8);
} else {
throw new Exception(response.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return result;
}

public static String get(String url, Map<String, Object> params) throws Exception {
String result = "";
HttpClient client = HttpClients.createDefault();

if (params != null && !params.isEmpty()) {
StringBuffer buffer = new StringBuffer();
buffer.append("?");
for (Map.Entry<String, Object> entry : params.entrySet()) {
String name = entry.getKey();
String value = URLEncoder.encode(entry.getValue().toString(), UTF_8);
buffer.append(name).append("=").append(value).append("&");
}
url += buffer.substring(0, buffer.length()-1).toString();
}

HttpGet get = new HttpGet(url);
get.setHeader("charset", UTF_8);

try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), UTF_8);
} else {
throw new Exception(response.toString());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}


编写测试代码

import java.util.HashMap;
import java.util.Map;

public class HttpTest {

public static void main(String[] args) {

String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("mobileCode", "1888888");
params.put("userID", "");
String result = HttpUtils.get(url, params);
System.out.println("GET请求:\n" + result);
result = HttpUtils.post(url, params);
System.out.println("POST请求:\n" + result);
} catch (Exception e) {
e.printStackTrace();
}
}

}


测试结果

GET请求:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">1888888:北京 北京 北京移动全球通卡</string>
POST请求:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">1888888:北京 北京 北京移动全球通卡</string>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: