post请求测试软件,简单的Http请求测试工具(支持get,post)

简介这篇文章主要介绍了简单的Http请求测试工具(支持get,post)以及相关的经验技巧,文章约7417字,浏览量296,点赞数5,值得参考!

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 mPropertys;

HttpClient(URL url) {

mURL = url;

mPropertys = new HashMap();

}

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值