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 nvps = new ArrayList();
                    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 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(); 

你可能感兴趣的:(httpclient的几种post参数格式)