通过OkHttpClient实现以json⽂件的形式发送http请求 近⽇做了个接⼝需求,要求把json数据放⼊⽂件中,以⽂件的形式发送http报⽂请求,在⽹上了好多种⽅式,都没能联调成功,最终⽤了OkHttpClient才实现了。 测试如下
util⼯具类
直接复制过去即可,什么都不⽤改。
import JSON;
import JSONArray;
import JSONObject;
import*;
import*;
import*;
import InetAddress;
import Socket;
import UnknownHostException;
import KeyManagementException;
import KeyStore;
import NoSuchAlgorithmException;
import CertificateException;
import X509Certificate;
import Matcher;
import Pattern;
public class OkHttpUtil {
public static class SSLSocketFactoryImp extends SSLSocketFactory {
角蜡蚧private SSLContext sslContext = Instance("SSL");
private TrustManager trustManager =null;
public SSLContext getSSLContext(){
return sslContext;
}
public X509TrustManager getTrustManager(){
return(X509TrustManager) trustManager;
}
public SSLSocketFactoryImp(KeyStore keyStore)throws NoSuchAlgorithmException, KeyManagementException {
super();
trustManager =new X509TrustManager(){
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s)throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s)throws CertificateException {
}
@Override
public X509Certificate[]getAcceptedIssuers(){
//注意这⾥不能返回null,否则会报错,如下⾯错误[1] X509Certificate[] x509Certificates =new X509Certificate[0];
return x509Certificates;
}
};
sslContext.init(null,new TrustManager[]{trustManager},null);
}
@Override
public String[]getDefaultCipherSuites(){
return new String[0];
}
@Override
public String[]getSupportedCipherSuites(){
return new String[0];
}
@Override
public Socket createSocket()throws IOException {
SocketFactory().createSocket();
}
@Override
public Socket createSocket(Socket socket, String host,int post,boolean autoClose)throws IOException {
SocketFactory().createSocket(socket, host, post, autoClose);
}
@Override
public Socket createSocket(String s,int i)throws IOException, UnknownHostException {
return null;
}
@Override
public Socket createSocket(String s,int i, InetAddress inetAddress,int i1)throws IOException, UnknownHostException { return null;
}
@Override
public Socket createSocket(InetAddress inetAddress,int i)throws IOException {
return null;
}
@Override
public Socket createSocket(InetAddress inetAddress,int i, InetAddress inetAddress1,int i1)throws IOException {
return null;
}
}
public static OkHttpClient interceptor){
OkHttpClient.Builder builder =null;
try{
builder =new OkHttpClient.Builder();
//ssl verifier
KeyStore trustStore;
trustStore = Instance(KeyStore
.getDefaultType());
trustStore.load(null,null);
SSLSocketFactoryImp ssl =new DefaultType()));
HostnameVerifier DO_NOT_VERIFY =new HostnameVerifier(){
@Override
public boolean verify(String hostname, SSLSession session){
return true;
}
};
builder.SSLContext().getSocketFactory(), TrustManager()) .hostnameVerifier(DO_NOT_VERIFY);
}catch(Exception e){
//
}
return builder.build();
}
}
Junit测试
public class haha{
public static void main(String[] args){
//1、先调⽤认证接⼝
String APIUrl ="xxxxxxxxxxxx";
String URL =""+""+":"+"xxxx"+ APIUrl;
自动排焊机String username ="xxx";
String pwd ="xxx";
JSONObject json =new JSONObject();
json.put("userName",username);
json.put("password",pwd);
// ⽣成json格式⽂件
拖曳声纳JSONObject job =doFilePost(json,URL);
System.out.println("调⽤认证接⼝:"+job);
}
public static JSONObject doFilePost(JSONObject params,String url){
try{
//在项⽬根⽬录下创建⼀个⽂件
File file =new File("file/post_request.json");
if(!ParentFile().exists()){// 如果⽗⽬录不存在,创建⽗⽬录 涂布纸
}
ists()){// 如果已存在,删除旧⽂件
file.delete();
}
/
/将格式化后的json字符串写⼊⽂件
Writer write =new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
write.JSONString());
write.flush();
write.close();
OkHttpClient client =getClient();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body =new MultipartBody.Builder().setType(MultipartBody.FORM)
.addFormDataPart("filename", Path(),
new Path())))
.
build();
Request request =new Request.Builder()
.url(url)
.method("POST", body)
.addHeader("Accept-Language","zh-cn")
.addHeader("Cookie","SESSIONID=765c194a6ab8b92845717cc4033a024d; C_HASH=605DA24B")
.build();
Response response = wCall(request).execute();
JSONObject job = JSON.parseObject(response.body().string());
return job;
飞盘制作
}catch(Exception e){
e.printStackTrace();
return null;
磁悬浮鼠标}
}
}