httpclient的几种post参数格式

1、json格式

HttpClientContext context = HttpClientContext.create();

                httpPost.setURI(java.net.URI.create(url));

                if(null != headers) {
                    for (String name : headers.keySet()) {
                        httpPost.setHeader(name, headers.get(name));
                    }
                }

                StringEntity entity1 = new StringEntity(json, HTTP.UTF_8);
                httpPost.setEntity(entity1);
                entity1.setContentType("application/json");


                System.out.println("Executing request " + httpPost.getRequestLine() + " to " +  " via " + proxy);

                CloseableHttpResponse response = httpClient.execute(httpPost,context);
                response.setLocale(Locale.CHINESE);

                long  end = System.currentTimeMillis();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

                if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                    entity = response.getEntity() ;

                    String html = EntityUtils.toString(entity, charSet);
                    response.close();

                    entity.getContent().close();
                }

2、urlencoded格式

HttpClientContext context = HttpClientContext.create();

                httpPost.setURI(java.net.URI.create(url));

                if(null != headers) {
                    for (String name : headers.keySet()) {
                        httpPost.setHeader(name, headers.get(name));
                    }
                }


                StringEntity entity1 = new StringEntity(postDataEncode, HTTP.UTF_8);

                entity1.setContentType("application/x-www-form-urlencoded");

                httpPost.setEntity(entity1);

                System.out.println("Executing request " + httpPost.getRequestLine() + " to " +  " via " + proxy);

                CloseableHttpResponse response = httpClient.execute(httpPost,context);
                response.setLocale(Locale.CHINESE);

                long  end = System.currentTimeMillis();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");

                if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                    entity = response.getEntity() ;

                    String html = EntityUtils.toString(entity, charSet);
                    response.close();

                    entity.getContent().close();
                }

3、key-value格式

HttpClientContext context = HttpClientContext.create();

                httpPost = new HttpPost(url);

                if(null != headers) {
                    for (String name : headers.keySet()) {
                        httpPost.setHeader(name, headers.get(name));
                    }
                }

                if(null != params) {
                    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                    for (String name : params.keySet()) {
                        nvps.add(new BasicNameValuePair(name, params.get(name)));
                    }
                    httpPost.setEntity(new UrlEncodedFormEntity(nvps));
                }


                System.out.println("Executing request " + httpPost.getRequestLine() + " to " +  " via " + proxy);

                CloseableHttpResponse response = httpClient.execute(httpPost,context);
                response.setLocale(Locale.CHINESE);

                String redirectUrl = "";
                List<URI> redirectUrls = context.getRedirectLocations();
                if(redirectUrls!=null){
                    redirectUrl = redirectUrls.get(0).toString();
                    System.out.println("重定向:" + redirectUrl);
                }

                long  end = System.currentTimeMillis();
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine() + "cost:" + (end-start)/1000 + "秒");
//                System.out.println(EntityUtils.toString(response.getEntity()));

                if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                    entity = response.getEntity() ;

                    String html = EntityUtils.toString(entity, charSet);
                    response.close();
//                  httpGet.releaseConnection();

                    entity.getContent().close();

                }

4、MultipartForm

 CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080" +
                    "/servlets-examples/servlet/RequestInfoExample");

            FileBody bin = new FileBody(new File(args[0]));
            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("bin", bin)
                    .addPart("comment", comment)
                    .build();


            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

5、XML格式

 File input = new File(“test.xml”);
      PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);

      // 设置请求的内容直接从文件中读取
      post.setRequestBody( new FileInputStream(input)); 
      if (input.length() < Integer.MAX_VALUE)
         post.setRequestContentLength(input.length());
      else
         post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);

      // 指定请求内容的类型
      post.setRequestHeader( "Content-type" , "text/xml; charset=GBK" );
      HttpClient httpclient = new HttpClient();
      int result = httpclient.executeMethod(post);
      System.out.println( "Response status code: " + result);
      System.out.println( "Response body: " );
      System.out.println(post.getResponseBodyAsString()); 
      post.releaseConnection(); 
  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: HttpClient是一个强大的基于HTTP协议的客户端编程工具,用于提供交互性的Web应用程序。而HttpClient参数格式指的是在使用HttpClient进行网络请求时,需使用的参数格式标准,也是HTTP请求信息中实体部分的内容。 HttpClient参数格式通常采用的是JSON或者XML格式,JSON格式常用于数据传输场景,XML格式则主要用于文本传输场景。对于传输的参数内容而言,Json格式可以在对象层级之间进行深度嵌套,不同的数据结构很方便地组合在一起,可以灵活处理各种数据类型;而XML格式则使用XML标签进行标记化,易于理解和扩展,同时支持多种编码方式。 在使用HttpClient时,需要将请求参数按照其格式进行编码,以方便后续的传输和处理。在编写代码时,HttpClient提供了自带的参数格式化方法,开发人员可以方便地调用这些方法,快速编写出高效稳定的网络请求代码。 总之,HttpClient参数格式是指在使用HttpClient进行网络请求时,需要按照格式标准对传输的参数内容进行编码,以方便后续的传输和处理,在实际的开发中,开发人员还需根据具体业务需求和数据类型,选择适合的格式进行编码。 ### 回答2: HttpClient是一个Java HTTP客户端库,用于发送HTTP请求和处理响应。它提供了强大的API和自定义配置选项来满足各种HTTP客户端需求。 HttpClient使用NameValuePair作为请求参数格式。NameValuePair是一组键值对,由NameValuePair类型的对象组成。每个NameValuePair对象代表一个参数,其中“name”是参数的名称,“value”是参数的值。可以使用List<NameValuePair>或NameValuePair数组来传递请求参数。 例如,一个简单的HTTP POST请求可以像这样使用HttpClient: List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", "my_username")); params.add(new BasicNameValuePair("password", "my_password")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); HttpPost post = new HttpPost("http://example.com/login"); post.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(post); 在上面的示例中,我们创建了一个带有两个参数(用户名和密码)的POST请求。我们使用UrlEncodedFormEntity将参数编码为HTTP表单格式并将其设置为请求实体。然后,我们创建一个HttpPost实例并使用其setEntity方法将请求实体设置为POST请求的实体。最后,我们使用DefaultHttpClient执行POST请求,并获取HttpResponse对象以获取响应。 总之,HttpClient使用NameValuePair作为请求参数格式,NameValuePair是由名称和值组成的键值对,通过List<NameValuePair>或NameValuePair数组传递。它提供了强大的API和自定义配置选项来满足各种HTTP客户端需求。 ### 回答3: HttpClient参数格式主要有三种:Query Parameter、 Path Parameter 和 Body Parameter。 Query Parameter 是在 URL 中通过 ?key=value 的形式,将键值对作为查询字符串传递给服务器。这种形式通常用于 GET 请求。 Path Parameter 通常用于 RESTful API,作为请求路径的一部分。例如 /api/users/{id},其中 {id} 就是一个 Path Parameter,表示要查询的用户的 ID。 Body Parameter 是将参数放在请求体中向服务器发送数据。在请求头中需要设置 Content-Type 为 application/json 或 application/x-www-form-urlencoded,然后将参数以 JSON 或 URL 编码的形式放在请求体中。 以上三种参数格式可以根据实际需求灵活使用,在开发和使用 HttpClient 时需要注意格式的正确性和请求头的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值