HttpURLConnection PUT请求

HttpURLConnection的put请求,链接中拼接参数+body传递参数

XX:在网上找了不少资料,没有找到自己想要的,自己调通了一个,在此记录!

JAVA代码

/*

*  httpUrl 服务器地址

*  jsonParam 请求参数JSON

*  authorization Token 如果没有token可以不传

*/

public static String getUploadInformation(String httpUrl, String jsonParam,String authorization) {
          PrintWriter out = null;
          BufferedReader in = null;
          String result = "";
          URL url = null;
          try {
                    url = new URL(httpUrl);

          } catch (Exception e) {
                    e.printStackTrace();
          }
          System.out.println(url);
          if (url != null) {
                    try {
                              HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                              conn.setRequestMethod("PUT");

                              conn.setRequestProperty("Content-Type", " application/json");// 设定

                              // 请求格式

                              // 设置通用的请求属性

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

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

                              conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

                              conn.setRequestProperty("Authorization", authorization); //  TOKEN 如果没有token可以删除

                              // 发送POST请求必须设置如下两行

                              conn.setDoOutput(true);

                              conn.setDoInput(true);

                              // 获取URLConnection对象对应的输出流

                              out = new PrintWriter(conn.getOutputStream());

                              // 发送请求参数

                              out.print(jsonParam);

                              // flush输出流的缓冲

                              out.flush();

                              if (conn.getResponseCode() == 200) {

                                        // 定义BufferedReader输入流来读取URL的响应

                                        in = new BufferedReader(new InputStreamReader(

                                        conn.getInputStream()));

                                        String line;

                                        while ((line = in.readLine()) != null) {
                                                  result += line;
                                        }
                              } else {
                                        System.out.println("请求失败----Code:" + conn.getResponseCode()
                                        + "Message:" + conn.getResponseMessage());
                              }
                              conn.disconnect();// 断开连接
                    } catch (Exception e) {
                              e.printStackTrace();
                    }
          }
          return result;
}

main方法调用



public static void main(String[] args) {

          // put 请求

          String param = "{\"参数2\":\"参数值\",\"参数3\":\"参数值\"}";

          String result = getUploadInformation( "http://IP:端口/api/接口名?参数1=参数值",param, "Bearer " + token);

          System.out.println("返回结果------------" + result);

}

你可能感兴趣的:(HttpURLConnection PUT请求)