use of okhttp3.Cookie in project spring-framework by spring-projects.
the class WebClientIntegrationTests method cookies.
@Test
public void cookies() throws Exception {
this.server.enqueue(new MockResponse().setHeader("Content-Type", "text/plain").setBody("test"));
Mono<String> result = this.webClient.get().uri("/test").cookie("testkey", "testvalue").exchange().then(response -> response.bodyToMono(String.class));
StepVerifier.create(result).expectNext("test").expectComplete().verify(Duration.ofSeconds(3));
RecordedRequest recordedRequest = server.takeRequest();
Assert.assertEquals(1, server.getRequestCount());
Assert.assertEquals("/test", recordedRequest.getPath());
Assert.assertEquals("testkey=testvalue", recordedRequest.getHeader(HttpHeaders.COOKIE));
}
use of okhttp3.Cookie in project Gradle-demo by Arisono.
the class OkhttpUtilsMain method sendSysResquest.
/**
* Okhttp 异步请求
*/
public static void sendSysResquest() {
RequestBody formBody = new FormBody.Builder().add("username", "123").add("password", "df13edafsdddsads").build();
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();
Request request = new Request.Builder().url("http://localhost:8080/spring-mvc-showcase/client/info").header("cookie", "JSESSIONID=EB36DE5E50E342D86C55DAE0CDDD4F6D").addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
String json = response.body().string();
System.out.println(json);
} else {
System.out.println(JSON.toJSONString(response.code()));
}
}
@Override
public void onFailure(Call call, IOException e) {
System.out.println(ExceptionUtils.printExceptionStack(e));
if (e instanceof ConnectException) {
System.out.println("服务器拒绝访问!");
} else if (e instanceof SocketTimeoutException) {
System.out.println("超时响应!");
}
}
});
}
use of okhttp3.Cookie in project Gradle-demo by Arisono.
the class OkhttpUtilsMain method sendHeadersAndParams.
/**
* 发请求头以及请求参数
*/
public static void sendHeadersAndParams() {
String china_str = "";
try {
china_str = URLEncoder.encode("中文", "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
MediaType MEDIA_TYPE_TEXT = MediaType.parse("text/plain");
String postBody = "Hello World";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://localhost:8080/spring-mvc-showcase/http/getHeaders").header("cookie", "JSESSIONID=EB36DE5E50E342D86C55DAE0CDDD4F6D").addHeader("content-type", "text/html;charset:utf-8").addHeader("Home", "china").addHeader("Home1", china_str).addHeader("user-agent", "android").put(RequestBody.create(MEDIA_TYPE_TEXT, postBody)).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String json = response.body().string();
System.out.println(json);
String home1 = JSON.parseObject(json).getJSONObject("headers").getString("home1");
System.out.println(URLDecoder.decode(home1, "utf-8"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of okhttp3.Cookie in project Gradle-demo by Arisono.
the class OkhttpUtilsMain method sendHeaders.
/**
* 发请求头
*/
public static void sendHeaders() {
String china_str = "";
try {
china_str = URLEncoder.encode("中文", "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://192.168.253.132:9090/platform-b2c/?client=true").header("cookie", "JSESSIONID=EB36DE5E50E342D86C55DAE0CDDD4F6D").addHeader("content-type", "text/html;charset:utf-8").addHeader("client-name", // 自定义的header
"uasClient").addHeader("Home1", // 自定义的header 传中�?
china_str).addHeader("user-agent", "android").build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
String json = response.body().string();
System.out.println(json);
String home1 = JSON.parseObject(json).getJSONObject("headers").getString("home1");
System.out.println(URLDecoder.decode(home1, "utf-8"));
} else {
System.out.println(JSON.toJSONString(response.code()));
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of okhttp3.Cookie in project Gradle-demo by Arisono.
the class OkhttpUtils method sendPostHttp.
/**
* post http
* @param url
* @param params
* @param tag
*/
public static void sendPostHttp(String url, Map<String, Object> params, String cookies, String tag) {
Builder paramBuilder = new FormBody.Builder();
if (!params.isEmpty()) {
Iterator<Map.Entry<String, Object>> entries = params.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, Object> entry = entries.next();
paramBuilder.add(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
OkhttpUtils.println(tag + ":" + url);
RequestBody formBody = paramBuilder.build();
Request request = new Request.Builder().url(url).addHeader("content-type", "text/html;charset:utf-8").addHeader("Cookie", cookies).post(formBody).build();
OkhttpUtils.client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) throws IOException {
String requestJson = OkhttpUtils.getResponseString(response);
RxBus.getInstance().send(tag + ":" + requestJson);
}
@Override
public void onFailure(Call call, IOException e) {
OkhttpUtils.onFailurePrintln(call, e, this);
}
});
}
}
Aggregations