# 请求示例
- java示例:
maven依赖:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
代码
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.Data;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.Base64;
public class Demo {
public static final String URL = "https://tosb-gateway.wlhyos.pre.xiaokuaikeji.com/tosb/";
public static final String APPID = "khy21062900750001";
public static final String SECRET = "638328d345a04f4f9aa8af6b0c79f536";
public static void main(String[] args) throws Exception {
Driver driver = new Driver();
driver.setName("张三");
driver.setPhone("13500000000");
driver.setIdCard("450902193607014875");
// 身份证照片,先上传然后获取文件名之后再填充
String idCardFrontFilename = uploadFile(getFileInputStream(), "身份证.jpeg");
driver.setIdCardFrontFilename(idCardFrontFilename);
String driverJson = JSON.toJSONString(driver);
String result = postJson("driver/create", driverJson);
}
public static String postJson(String method, String jsonStr) throws Exception {
CloseableHttpClient sslClient = sslClient();
CloseableHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(URL + method);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("appid", APPID);
httpPost.setHeader("sign", getHmacSign(jsonStr, SECRET));
StringEntity se = new StringEntity(jsonStr, "UTF-8");
httpPost.setEntity(se);
response = sslClient.execute(httpPost);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
return EntityUtils.toString(resEntity, "UTF-8");
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (sslClient != null) {
sslClient.close();
}
if (response != null) {
response.close();
}
}
return null;
}
public static String uploadFile(InputStream fileInputStream, String uploadFilename) throws Exception{
HttpPost httpPost = new HttpPost(URL + "/file/uploadFile");
CloseableHttpClient httpClient = sslClient();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
httpPost.addHeader("appid", APPID);
httpPost.addHeader("sign", getHmacSign("{}", SECRET));
builder.addBinaryBody("file", fileInputStream, ContentType.MULTIPART_FORM_DATA, uploadFilename);
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(httpPost);
String returnStr = EntityUtils.toString(response.getEntity(), "utf-8");
response.close();
httpClient.close();
Result<String> result = JSON.parseObject(returnStr, new TypeReference<Result<String>>() {
});
if (!result.getCode().equals("00000")) {
throw new RuntimeException("上传失败, 结果:" + returnStr);
}
return result.getData();
}
public static InputStream getFileInputStream() throws Exception {
// 本地文件
File f = new File("/Users/hello/wecom-temp-fc587837dbaccc339ef249d2765f3749.jpg");
return new FileInputStream(f);
// 网络文件
/*
HttpClient client = HttpClients.createDefault();
HttpGet httpget = new HttpGet("url");
HttpResponse response = client.execute(httpget);
HttpEntity entity = response.getEntity();
return entity.getContent();*/
}
/**
* 签名
*
*/
private static String getHmacSign(String content, String secret) {
byte[] result = null;
try {
SecretKeySpec signinKey = new SecretKeySpec(secret.getBytes(), "HmacSHA1");
Mac mac = Mac.getInstance("HmacSHA1");
mac.init(signinKey);
byte[] rawHmac;
rawHmac = mac.doFinal(content.getBytes(StandardCharsets.UTF_8));
result = Base64.getEncoder().encode(rawHmac);
} catch (Exception e) {
throw new RuntimeException(e);
}
if (null != result) {
return new String(result);
} else {
return null;
}
}
private static CloseableHttpClient sslClient() {
SSLContext ctx = SSLContexts.createDefault();
X509TrustManager tm = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
try {
ctx.init(null, new TrustManager[]{tm}, new SecureRandom());
} catch (Exception e) {
throw new RuntimeException(e);
}
SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(ctx);
return HttpClients.custom().setSSLSocketFactory(factory).build();
}
@Data
public static class Driver {
private String name;
private String phone;
private String idCard;
// 身份证照片
private String idCardFrontFilename;
}
@Data
public static class Result<T> {
private String code;
private T data;
private String message;
}
}