JAVA後臺HTTP POST請求模擬表單FORM-DATA格式獲取數據的方法

package szsti.common.utils;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import org.apache.http.entity.ContentType;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;

import java.io.*;

/**
 * <dependencies>
 * <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
 * <dependency>
 *     <groupId>org.apache.httpcomponents</groupId>
 *     <artifactId>httpclient</artifactId>
 *     <version>4.5.2</version>
 * </dependency>
 *
 * <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpmime -->
 * <dependency>
 *     <groupId>org.apache.httpcomponents</groupId>
 *     <artifactId>httpmime</artifactId>
 *     <version>4.5.2</version>
 * </dependency>
 *
 * <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
 * <dependency>
 *     <groupId>org.apache.httpcomponents</groupId>
 *     <artifactId>httpcore</artifactId>
 *     <version>4.4.11</version>
 * </dependency>
 *
 */
public class PostMultiData {
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            String uri = "http://127.0.0.1:8080/requrl";
//            uri="http://127.0.0.1:8080/";
            HttpPost httppost = new HttpPost(uri);

            String user="qy02";
            String pwd="a88888888";
            String cduuid="DB7C2ED2140C2A3D6A6F5801FFE0347F";
            String key="jinting";
            String time=(System.currentTimeMillis() / 1000)-50+"";//獲取系統時間戳

            String token=MD5Utils.Encrypt(user+pwd+cduuid+key+time+"546f85d1-1f44-4206-85cd-3449d68fdfd0",false);
//          這裏的是有附件的情況的請求提交
//            String path="C:/Users/Administrator/Desktop/study/微服務/netflix/eureka.png";
//            File file = new File(path);
//            FileBody bin = new FileBody(file);
            StringBody user1 = new StringBody(user, ContentType.TEXT_PLAIN);
            StringBody pwd1 = new StringBody(pwd, ContentType.TEXT_PLAIN);
            StringBody cduuid1 = new StringBody(cduuid, ContentType.TEXT_PLAIN);
            StringBody key1 = new StringBody(key, ContentType.TEXT_PLAIN);
            StringBody time1 = new StringBody(time, ContentType.TEXT_PLAIN);
            StringBody token1 = new StringBody(token, ContentType.TEXT_PLAIN);


            HttpEntity reqEntity = MultipartEntityBuilder.create()
                   // .addPart("bin", bin)
                  //  .addPart("comment", comment)
                    .addPart("user",user1)
                    .addPart("pwd",pwd1)
                    .addPart("cduuid",cduuid1)
                    .addPart("key",key1)
                    .addPart("time",time1)
                    .addPart("token",token1)
                    .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());
                    System.out.println("Response content length: " + resEntity.getContent());
                    String a=EntityUtils.toString(resEntity);
                    //打印獲取到的返回值
                    System.out.println("Response content: " + a);
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章