PostMan传参数
证明对方接口是通的
通过postman测试接口,没有问题。
java代码实现
一、相关java依赖
<!--http-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.9</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.12</version>
</dependency>
二、java代码实现
package com.example.http.util;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class HttpUtil {
public static void test() {
String goodsUrl = "https://127.0.0.1:8080/updateRSA";
//本地文件位置(data 是加密后的密文,明文也是一样的主要看接收方)
String data ="JHKPvMMye8XN06krSE/3wMRMic6b4jxkndNSsoJ32HBTppxAvEsreEDtd6dqzyKkIaEE4nXMiUZl1K7/cpInjJbHgi0mRbgBX/mhVQz+gFYgvKeheFbebDlrdSMRErSDUECQG1MDkbodeZcqC2C2uf2OEINW62iTrAHrTG/8H11Kz8zxHR2pdglTQOXw9/gZsiyXQAGWd3GF5b4eB71pmR7z/ghmdkvDmpO3ZyEvmLgrg148qAM/3SL2WaaCaHXF5XoAvCK3OBmdSNYMqpDwuYna3bbryV1NfvvFkKKCETVTjV3IfeRGIGcU9C23tvk3BypjpNPSHpslnGOIk/MOKg==";
String str = null;
try {
//添加请求头
HashMap<String, String> map = new HashMap<>();
// map.put("token", CommonConstant.token);
// File file = new File(fileName);
// str = doPostUploadFile(goodsUrl + "/sacw/CommonConfig/uploadFile", map, file);
str = doPostUploadFile(goodsUrl, map,data);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* post请求提交form-data上传文件
* @param url 上传地址
* @param headers 请求头
* @param file 上传文件
* @return
*/
public static String doPostUploadFile(String url, Map<String, String> headers,String data /*,File file*/){
HttpPut httpPost = new HttpPut(url);
packageHeader(headers, httpPost);
// String fileName = file.getName();
CloseableHttpResponse response = null;
String respContent = null;
long startTime = System.currentTimeMillis();
// 设置请求头 boundary边界不可重复,重复会导致提交失败
String boundary = "-------------------------" + UUID.randomUUID().toString();
httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
// 创建MultipartEntityBuilder
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
// 设置字符编码
builder.setCharset(StandardCharsets.UTF_8);
// 模拟浏览器
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// 设置边界
builder.setBoundary(boundary);
builder.addTextBody("data", data);
// 设置multipart/form-data流文件
// builder.addPart("sendfile", new FileBody(file));
// application/octet-stream代表不知道是什么格式的文件
// builder.addBinaryBody("media", file, ContentType.create("application/octet-stream"), fileName);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
response = httpClient.execute(httpPost);
if (response != null && response.getStatusLine() != null && response.getStatusLine().getStatusCode() < 400) {
HttpEntity he = response.getEntity();
if (he != null) {
String string = EntityUtils.toString(he, "UTF-8");
JSONObject jsonObject = JSONObject.fromObject(string);
Object data1 = jsonObject.get("data");
System.out.println("data:"+data1);
System.out.println("result:"+jsonObject);
}
} else {
throw new RuntimeException();
}
return respContent;
} catch (Exception e) {
throw new RuntimeException();
} finally {
try {
if (response != null) {
response.close();
}
if(null != httpClient){
httpClient.close();
}
} catch (IOException e) {
}
}
}
/**
* 封装请求头
* @param paramsHeads
* @param httpMethod
*/
private static void packageHeader(Map<String, String> paramsHeads, HttpPut httpMethod) {
if (null!= paramsHeads && paramsHeads.size()>0) {
Set<Map.Entry<String, String>> entrySet = paramsHeads.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
httpMethod.setHeader(entry.getKey(), entry.getValue());
}
}
}
public static void main(String[] args) {
test();
}
}
用这种方式封装参数,无论怎么发,接口是通但是对方一直接收不到参数
//创建post方式请求对象
HttpPut httpPost = new HttpPut(url);
//装填参数
// jsonObject 是需要封装的参数
StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
//设置参数到请求对象中
httpPost.setEntity(s);
//设置header信息
//指定报文头【Content-type】、【User-Agent】
//httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("enctype","multipart/form-data");
什么方法都可以,如果发送的是Psot请求,直接New一个HttpPost请求即可,我这边第三方接口用的是put,所以,我new的就是HttpPut接口。