写着这篇文章,心里却想着贝叶斯公式,以前就是简单粗暴的计算概率,却忘记了还有先验概率,似然概率,归一化因子来计算:后验概率。
以前项目的接口测试都是基于Http,现在REST接口测试都是基于Https, 所以需要将目前测试代码中Jetty的Client中的HTTP方式改成Https的方式。
查找Jetty的官网 Jetty Http Client Study,里面很清楚的介绍到如何设置HTTPS的请求:
In order to perform HTTPS requests, you should create first a SslContextFactory
, configure it, and pass it to the HttpClient
constructor. When created with a SslContextFactory
, the HttpClient
will be able to perform both HTTP and HTTPS requests to any domain.
// Instantiate and configure the SslContextFactory
SslContextFactory sslContextFactory = new SslContextFactory();
// Instantiate HttpClient with the SslContextFactory
HttpClient httpClient = new HttpClient(sslContextFactory);
// Start HttpClient
httpClient.start();
- 如果遇到执行Https Request提示Too Large的错误提示,那么需要设置Request的Buffer大小,默认Request的Buffer大小是4096Bytes, 可以在源码里找到:
private int requestBufferSize = 4096;
所以,需要增加Request的Buffer, 例如:
httpclient.setRequestBufferSize(2*4096);
这里要划下重点!!!
- 如果遇到执行过程里提示“Authentication challenge without WWW-Authenticate header”的错误。
提示:可以进一步打断点到Jetty的源码,看看底层源码发生了什么错误。最后发现这个错误就是因为Client Request发往Server后,被Server告知401未授权。
服务器会返回一个401 Unauthozied给客户端,并且在Response 的 header "WWW-Authenticate" 中添加信息。
贴下jetty Https的实现
package base;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.util.BytesContentProvider;
import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import java.util.HashMap;
public class HTTPClientRequest {
public static HashMap RequestToPlatform(String method, String requestBody, String URL) throws Exception{
String token = PostResult2File.ReadFile("src/test/resources/Readtoken.dat");
String[] tokenValue = token.split(",");
HashMap<String,Object> responseMap = new HashMap<>();
SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setTrustAll(false);
System.setProperty("javax.net.ssl.trustStore","src/test/resources/mg.store");
HttpClient httpclient = new HttpClient(sslContextFactory);
//httpclient.setFollowRedirects(false);
httpclient.setRequestBufferSize(2*4096);
httpclient.start();
ContentResponse response = null;
System.out.println("====Sending Http Request====");
//add token into header
if (method.toLowerCase().equals("post") || method.toLowerCase().equals("put")){
response = httpclient.newRequest(URL).header("Authorization",tokenValue[1]).method(HttpMethod.POST).content(new BytesContentProvider(requestBody.getBytes("UTF-8")),"application/json;charset=UTF-8").send();
//response = httpclient.newRequest(URL).method(HttpMethod.POST).content(new BytesContentProvider(requestBody.getBytes("UTF-8")),"application/json;charset=UTF-8").send();
System.out.println(response.getHeaders());
}
if (method.toLowerCase().equals("get")){
response = httpclient.newRequest(URL).header("Authorization",tokenValue[1]).method(HttpMethod.GET).send();
}
if(response!=null){
responseMap.put("ResponseStatus",response.getStatus());
responseMap.put("ResponseBody", response.getContentAsString());
}
return responseMap;
}
}
网友评论