import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public final class HttpClient {
public static final String HTTP_METHOD_POST = "POST";
public static final String HTTP_METHOD_GET = "GET";
public static final String HTTP_METHOD_PUT = "PUT";
public static final String HTTP_METHOD_DELETE = "DELETE";
public static final Integer CONNECT_TIME_OUT = 10000;
public static HttpClient create(URL url) {
return new HttpClient(url);
}
URL mURL;
String mMethod = HTTP_METHOD_GET;
String mData = "";
Map<String, String> mPropertys;
HttpClient(URL url) {
mURL = url;
mPropertys = new HashMap<String, String>();
}
public synchronized void setRequestMethod(String method) {
if (method == null || "".equals(method))
return;
if (HTTP_METHOD_POST == method || HTTP_METHOD_GET == method
|| HTTP_METHOD_PUT == method || HTTP_METHOD_DELETE == method) {
mMethod = method;
}
}
public synchronized void setRequestData(String data) {
if (data == null)
return;
mData = data;
}
public synchronized void setRequestProperty(String name, String value) {
if (name == null || "".equals(name) || value == null
|| "".equals(value))
return;
mPropertys.put(name, value);
}
public synchronized String execute() throws IOException {
return execute("utf-8",CONNECT_TIME_OUT);
}
public synchronized String execute(String encode) throws IOException {
return execute(encode,CONNECT_TIME_OUT);
}
public synchronized String execute(String encode,Integer timeout) throws IOException {
HttpURLConnection conn = (HttpURLConnection) mURL.openConnection();
conn.setRequestMethod(mMethod);
conn.setDoInput(true);
conn.setConnectTimeout(timeout);
if (HTTP_METHOD_POST == mMethod) {
conn.setRequestProperty("Content-type","application/json");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Length",
String.valueOf(mData.getBytes().length));
conn.getOutputStream().write(mData.getBytes(encode));
}
int code = conn.getResponseCode();
System.out.println("****code:"+code);
if (code == HttpURLConnection.HTTP_OK) {
InputStreamReader isr = new InputStreamReader(
conn.getInputStream(),encode);
BufferedReader in = new BufferedReader(isr);
StringBuffer sbuf = new StringBuffer();
String inputLine = null;
while ((inputLine = in.readLine()) != null) {
sbuf.append(inputLine);
}
return sbuf.toString();
}
return null;
}
public static void main(String[] args) {
try {
String temp="{ \"username\":\"test\",\"password\":\"123\"}"; //json数据
HttpClient http = new HttpClient(new URL(
"http://.........."));
http.setRequestMethod(HttpClient.HTTP_METHOD_POST);
http.setRequestData(temp);
String respCode = new String(http.execute());
System.out.println("****finished,respCode:" + respCode);
} catch (Exception e) {
System.out.println(e);
}
}
}
简单的Http请求测试工具(支持get,post)
Posted dw89
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单的Http请求测试工具(支持get,post)相关的知识,希望对你有一定的参考价值。
以上是关于简单的Http请求测试工具(支持get,post)的主要内容,如果未能解决你的问题,请参考以下文章
.bat文件实现一个简单的http请求工具(支持get和post请求)
.bat文件实现一个简单的http请求工具(支持get和post请求)