java模拟发送form-data的请求

版权声明:feixie https://blog.csdn.net/qq_36850813/article/details/83828697
package com.silot.test;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class TestCli
{


    public static void main(String args[]) throws Exception
    {

        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "------------------------------0ea3fcae38ff", Charset.defaultCharset());
        multipartEntity.addPart("skey", new StringBody("哈哈哈哈哈", Charset.forName("UTF-8")));
        multipartEntity.addPart("operator", new StringBody("啦啦啦啦", Charset.forName("UTF-8")));
        multipartEntity.addPart("VrfKey", new StringBody("渣渣渣", Charset.forName("UTF-8")));
        multipartEntity.addPart("StatCode", new StringBody("00", Charset.forName("UTF-8")));
        multipartEntity.addPart("mass_id", new StringBody("1234", Charset.forName("UTF-8")));
        multipartEntity.addPart("reference_id", new StringBody("21231544", Charset.forName("UTF-8")));

        HttpPost request = new HttpPost("http://xiefei.s1.natapp.cc/v1/withdrawCallback");
        request.setEntity(multipartEntity);
        request.addHeader("Content-Type", "Content-Disposition: form-data; boundary=------------------------------0ea3fcae38ff");

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);

        InputStream is = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null)
        {
            buffer.append(line);
        }

        System.out.println("发送消息收到的返回:" + buffer.toString());
    }

}

猜你喜欢

转载自blog.csdn.net/qq_36850813/article/details/83828697