提交 09d997f1 编写于 作者: 小傅哥's avatar 小傅哥

feat:HTTP 框架对接测试

上级 39956fc7
package cn.bugstack.xfg.dev.tech.test;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
/**
* @author Fuzhengwei bugstack.cn @小傅哥
* @description 单元测试
* <dependency>
* <groupId>org.apache.httpcomponents</groupId>
* <artifactId>httpclient</artifactId>
* <version>4.5.14</version>
* </dependency>
* <dependency>
* <groupId>org.apache.httpcomponents</groupId>
* <artifactId>httpmime</artifactId>
* <version>4.5.10</version>
* </dependency>
* @create 2023-10-14 10:08
*/
@Slf4j
public class HttpClientTest {
@Test
public void test_httpClient_comments(){
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://api.zsxq.com/v2/topics/188451221521142/comments");
httpPost.setHeader("Accept-Encoding", "deflate, gzip");
httpPost.setHeader("accept", "application/json, text/plain, */*");
httpPost.setHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
httpPost.setHeader("authority", "api.zsxq.com");
httpPost.setHeader("content-type", "application/json");
httpPost.setHeader("cookie", "zsxq_access_token=86EB233E-BBD0-1E3C-941D-817CA1303C87_9D76421394C6F474");
httpPost.setHeader("dnt", "1");
httpPost.setHeader("origin", "https://wx.zsxq.com");
httpPost.setHeader("referer", "https://wx.zsxq.com/");
httpPost.setHeader("sec-ch-ua", "\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"");
httpPost.setHeader("sec-ch-ua-mobile", "?0");
httpPost.setHeader("sec-ch-ua-platform", "\"macOS\"");
httpPost.setHeader("sec-fetch-dest", "empty");
httpPost.setHeader("sec-fetch-mode", "cors");
httpPost.setHeader("sec-fetch-site", "same-site");
httpPost.setHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
httpPost.setHeader("x-request-id", "162ae5f17-2123-4ae3-67df-8b9775414e0");
httpPost.setHeader("x-signature", "698895e3ec4e651128b3d16755546bd2bc659687");
httpPost.setHeader("x-timestamp", "1697257286");
httpPost.setHeader("x-version", "2.45.0");
String requestBody = "{\"req_data\":{\"text\":\"chatglmt 回答测试\\n\",\"image_ids\":[],\"mentioned_user_ids\":[]}}";
try {
httpPost.setEntity(new StringEntity(requestBody));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
System.out.println(responseString);
// 关闭连接
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void test_httpClient() throws IOException {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet("https://api.zsxq.com/v2/groups/28885518425541/topics?scope=all&count=20");
get.addHeader("Accept-Encoding", "deflate, gzip");
get.addHeader("accept", "application/json, text/plain, */*");
get.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8");
get.addHeader("authority", "api.zsxq.com");
get.addHeader("cookie", "zsxq_access_token=86EB233E-BBD0-1E3C-941D-817CA1303C87_9D76421394C6F474");
get.addHeader("dnt", "1");
get.addHeader("origin", "https://wx.zsxq.com");
get.addHeader("referer", "https://wx.zsxq.com/");
get.addHeader("sec-ch-ua", "\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"");
get.addHeader("sec-ch-ua-mobile", "?0");
get.addHeader("sec-ch-ua-platform", "\"macOS\"");
get.addHeader("sec-fetch-dest", "empty");
get.addHeader("sec-fetch-mode", "cors");
get.addHeader("sec-fetch-site", "same-site");
get.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36");
get.addHeader("x-request-id", "372177b46-4e7d-9373-d891-98a22adaeb7");
get.addHeader("x-signature", "32b39b5d1af5995e3b5022e58a8d8f23cd427434");
get.addHeader("x-timestamp", "1697249698");
get.addHeader("x-version", "2.45.0");
CloseableHttpResponse response = httpClient.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String jsonStr = EntityUtils.toString(response.getEntity());
String jsonObj = StringEscapeUtils.unescapeJava(jsonStr);
log.info("拉取数据。jsonStr:{}", jsonObj);
} else {
throw new RuntimeException("Err Code is " + response.getStatusLine().getStatusCode());
}
}
}
package cn.bugstack.xfg.dev.tech.test;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
* @author Fuzhengwei bugstack.cn @小傅哥
* @description OKHttp
* @create 2023-10-14 11:26
*/
public class OKHttpTest {
@Test
public void test_okhttp() throws Exception {
OkHttpClient client = new OkHttpClient();
String url = "https://api.zsxq.com/v2/groups/28885518425541/topics?scope=all&count=20";
Request request = new Request.Builder()
.url(url)
.addHeader("Accept-Encoding", "deflate, gzip")
.addHeader("accept", "application/json, text/plain, */*")
.addHeader("accept-language", "zh-CN,zh;q=0.9,en;q=0.8")
.addHeader("authority", "api.zsxq.com")
.addHeader("cookie", "__cuid=5330a556392a4c5b8084b4cbc165e0f3; zsxq_access_token=86EB233E-BBD0-1E3C-941D-817CA1303C87_9D76421394C6F474; abtest_env=product; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22241858242255511%22%2C%22first_id%22%3A%2217ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217ebd0b4317ecb-0b27f672c2d3af-133a6253-1296000-17ebd0b4318ba7%22%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTgwMmQ2YjZiOWIxZjMtMGQ4YzMzZjhmYTA3YmEtMzU3MzZhMDMtMTI5NjAwMC0xODAyZDZiNmI5YzEwODYiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIyNDE4NTgyNDIyNTU1MTEifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22241858242255511%22%7D%7D; zsxqsessionid=4ac8449313681ca8b436d052eab5925e")
.addHeader("dnt", "1")
.addHeader("origin", "https://wx.zsxq.com")
.addHeader("referer", "https://wx.zsxq.com/")
.addHeader("sec-ch-ua", "\"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"")
.addHeader("sec-ch-ua-mobile", "?0")
.addHeader("sec-ch-ua-platform", "\"macOS\"")
.addHeader("sec-fetch-dest", "empty")
.addHeader("sec-fetch-mode", "cors")
.addHeader("sec-fetch-site", "same-site")
.addHeader("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36")
.addHeader("x-request-id", "372177b46-4e7d-9373-d891-98a22adaeb7")
.addHeader("x-signature", "32b39b5d1af5995e3b5022e58a8d8f23cd427434")
.addHeader("x-timestamp", "1697249698")
.addHeader("x-version", "2.45.0")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
ResponseBody responseBody = response.body();
String string = responseBody.string();
System.out.println(string);
}
});
// 等待回调
new CountDownLatch(1).await();
}
}
package cn.bugstack.xfg.dev.tech.test;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import org.junit.Test;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.jackson.JacksonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.Query;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @author Fuzhengwei bugstack.cn @小傅哥
* @description okhttp Retrofit2
* @create 2023-10-14 11:11
*/
public class Retrofit2Test {
@Test
public void test_retrofit() {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS);
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(loggingInterceptor);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.zsxq.com/")
.client(httpClient.build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
// .addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(JacksonConverterFactory.create())
.build();
ApiService apiService = retrofit.create(ApiService.class);
Call<String> call = apiService.getTopics("all", 20, "zsxq_access_token=86EB233E-BBD0-1E3C-941D-817CA1303C87_9D76421394C6F474;");
try {
String response = call.execute().body();
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
}
interface ApiService {
@GET("v2/groups/28885518425541/topics?scope=all&count=20")
@Headers({
"Accept-Encoding: deflate, gzip",
"accept: application/json, text/plain, */*",
"accept-language: zh-CN,zh;q=0.9,en;q=0.8",
"authority: api.zsxq.com",
"dnt: 1",
"origin: https://wx.zsxq.com",
"referer: https://wx.zsxq.com/",
"sec-ch-ua: \"Chromium\";v=\"118\", \"Google Chrome\";v=\"118\", \"Not=A?Brand\";v=\"99\"",
"sec-ch-ua-mobile: ?0",
"sec-ch-ua-platform: \"macOS\"",
"sec-fetch-dest: empty",
"sec-fetch-mode: cors",
"sec-fetch-site: same-site",
"user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36",
"x-request-id: 372177b46-4e7d-9373-d891-98a22adaeb7",
"x-signature: 32b39b5d1af5995e3b5022e58a8d8f23cd427434",
"x-timestamp: 1697249698",
"x-version: 2.45.0"
})
Call<String> getTopics(@Query("scope") String scope, @Query("count") int count, @Header("cookie") String cookie);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册