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

java中怎样用post、get、put请求

2016-06-02 11:35 330 查看
java中我们有时需要调用第三方的接口,但是第三方的接口都是http形式的,这是需要用post,get,put请求类型请求

1、GET方式请求

public static String httpGet(String getUrl,Map<String, String> getHeaders) throws IOException {

    URL getURL = new URL(getUrl);

    HttpURLConnection connection = (HttpURLConnection) getURL.openConnection();

    connection.setRequestProperty("accept", "*/*");

    connection.setRequestProperty("connection", "Keep-Alive");

    connection.setRequestProperty("user-agent",

            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");//在get请求中这是能在各个浏览器兼容json

    if(getHeaders != null) {

        for(String pKey : getHeaders.keySet()) {

            connection.setRequestProperty(pKey, getHeaders.get(pKey));

        }

    }

    connection.connect();

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

    StringBuilder sbStr = new StringBuilder();

    String line;

    while ((line = bufferedReader.readLine()) != null) {

        sbStr.append(line);

    }

    bufferedReader.close();

    connection.disconnect();

    return new String(sbStr.toString().getBytes(),"utf-8");

}

2、POST方式请求

    public static String httpPost(String postUrl,Map<String, String> postHeaders, String postEntity) throws IOException {

        URL postURL = new URL(postUrl);

        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();

        httpURLConnection.setDoOutput(true);                 

        httpURLConnection.setDoInput(true);

        httpURLConnection.setRequestMethod("POST");

        httpURLConnection.setUseCaches(false);

        httpURLConnection.setInstanceFollowRedirects(true);

        //application/json x-www-form-urlencoded

        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");

        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");

        StringBuilder sbStr = new StringBuilder();

        if(postHeaders != null) {

            for(String pKey : postHeaders.keySet()) {

                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));

            }

        }

        if(postEntity != null) {

             JSONObject obj = new JSONObject();

             obj.put("plate",postEntity);

            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   

            out.println(obj);  

            out.close();  

            //httpURLConnection.getInputStream()

            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));  //解决返回值汉字乱码的问题

              

            String inputLine;

            while ((inputLine = in.readLine()) != null) {  

                sbStr.append(inputLine);  

            }  

            in.close();  

        }

        httpURLConnection.disconnect();

        System.out.println("fsdfdf:"+sbStr.toString());

        return new String(sbStr.toString().getBytes(),"utf-8");

    }    
4000

3、PUT请求

    public static String HttpPut(String postUrl,Map<String, String> postHeaders,String postEntity) throws Exception {  

        URL postURL = new URL(postUrl);

        HttpURLConnection httpURLConnection = (HttpURLConnection) postURL.openConnection();

        httpURLConnection.setDoOutput(true);                 

        httpURLConnection.setDoInput(true);

        httpURLConnection.setRequestMethod("PUT");

        httpURLConnection.setUseCaches(false);

        httpURLConnection.setInstanceFollowRedirects(true);

        //application/json x-www-form-urlencoded

        //httpURLConnection.setRequestProperty("Content-Type",  "application/x-www-form-urlencoded");//表单上传的模式

        httpURLConnection.setRequestProperty("Content-Type",  "application/json;charset=utf-8");//json格式上传的模式

        StringBuilder sbStr = new StringBuilder();

        if(postHeaders != null) {

            for(String pKey : postHeaders.keySet()) {

                httpURLConnection.setRequestProperty(pKey, postHeaders.get(pKey));

            }

        }

        if(postEntity != null) {

             JSONObject obj = new JSONObject();

             obj.put("plate","京NB1314");

            PrintWriter out = new PrintWriter(new OutputStreamWriter(httpURLConnection.getOutputStream(),"utf-8"));   

            out.println(obj);  

            out.close();  

            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection  

                    .getInputStream()));  

              

            String inputLine;

            while ((inputLine = in.readLine()) != null) {  

                sbStr.append(inputLine);  

            }  

            in.close();  

        }

        httpURLConnection.disconnect();

        return new String(sbStr.toString().getBytes(),"utf-8");

    } 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: