码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-12-26 17:48:39      阅读:586      评论:0      收藏:0      [点我收藏+]

标签:input   cut   system   throws   net   请求   byte   new   exception   

 

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)

标签:input   cut   system   throws   net   请求   byte   new   exception   

原文地址:https://www.cnblogs.com/dw89/p/8118426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!