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

 

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

posted @ 2017-12-26 15:57  典韦叔叔  阅读(1433)  评论(0编辑  收藏  举报