美文网首页iapp学习
HttpURLConnection PUT请求

HttpURLConnection PUT请求

作者: Gxiner | 来源:发表于2017-06-25 19:06 被阅读0次

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请求

    HttpURLConnection的put请求,链接中拼接参数+body传递参数 XX:在网上找了不少资料,没有找...

  • HttpSimpleUtils

    功能: 基于HttpURLConnection 实现的常用 Http 常用方法: post get put del...

  • 网络技术-HttpURLConnection

    使用HttpURLConnection发送HTTP请求 获取HttpURLConnection对象。一般只需new...

  • HttpURLConnection基本使用

    1. HttpURLConnection基本使用 HttpURLConnection是网络请求的基本类,在具体了解...

  • PUT请求

    PUT请求和POST请求类似,只是PUT请求会把发送的文件放到服务器固定的路径里,而POST则只能把资源给服务器,...

  • PUT 请求

    NSURL *url = [NSURL URLWithString:@"你要请求的URL"]; NSMutable...

  • 用HttpURLConnection请求之post/get及多文

    目录:1、post和get请求2、使用HttpURLConnection请求multipart/form-data...

  • HttpURLConnection

    HttpURLConnection tags:java HTTP get/post请求 HttpURLConnec...

  • Android 网络(三) HttpURLConnection

    参考Android网络请求心路历程Android Http接地气网络请求(HttpURLConnection) 一...

  • OKHttp

    优点 支持http请求,https请求。 支持文件下载。 使用的是HttpURLConnection,不要担心an...

网友评论

    本文标题:HttpURLConnection PUT请求

    本文链接:https://www.haomeiwen.com/subject/nmyrcxtx.html