调整HttpRequest生成方式

old-package
lensfrex 2 years ago
parent c0d1d11693
commit b9e75fcd51
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 16
      mywust-core/src/main/java/cn/linghang/mywust/core/service/auth/AuthRequestFactory.java
  2. 12
      mywust-core/src/main/java/cn/linghang/mywust/core/service/auth/UnionLogin.java
  3. 7
      mywust-core/src/main/java/cn/linghang/mywust/core/service/library/LibraryAuthRequestFactory.java
  4. 14
      mywust-core/src/main/java/cn/linghang/mywust/core/service/library/LibraryLogin.java
  5. 18
      mywust-core/src/main/java/cn/linghang/mywust/core/service/undergraduate/BkjxAuthRequestFactory.java
  6. 28
      mywust-core/src/main/java/cn/linghang/mywust/core/service/undergraduate/JwcLogin.java
  7. 32
      mywust-network-okhttp/src/main/java/cn/linghang/mywust/network/okhttp/SimpleOkhttpRequester.java
  8. 48
      mywust-network/src/main/java/cn/linghang/mywust/network/HttpRequest.java
  9. 35
      mywust-network/src/main/java/cn/linghang/mywust/network/HttpResponse.java
  10. 27
      mywust-network/src/main/java/cn/linghang/mywust/network/RequestFactory.java
  11. 17
      mywust-network/src/main/java/cn/linghang/mywust/network/Requester.java
  12. 2
      mywust-test/src/test/java/JwcLoginTest.java

@ -1,13 +1,15 @@
package cn.linghang.mywust.core.service.auth; package cn.linghang.mywust.core.service.auth;
import cn.linghang.mywust.core.api.UnionAuth;
import cn.linghang.mywust.network.HttpRequest; import cn.linghang.mywust.network.HttpRequest;
import cn.linghang.mywust.network.RequestFactory;
import cn.linghang.mywust.util.StringUtil; import cn.linghang.mywust.util.StringUtil;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class AuthRequestFactory { public class AuthRequestFactory extends RequestFactory {
public static HttpRequest unionLoginTGTRequest(String username, String password, String service) { public static HttpRequest unionLoginTGTRequest(String username, String password, String service) {
Map<String, String> requestForm = new HashMap<>(4); Map<String, String> requestForm = new HashMap<>(4);
requestForm.put("username", username); requestForm.put("username", username);
@ -17,21 +19,15 @@ public class AuthRequestFactory {
String queryString = StringUtil.generateQueryString(requestForm); String queryString = StringUtil.generateQueryString(requestForm);
HttpRequest request = new HttpRequest(); return makeHttpRequest(UnionAuth.UNION_AUTH_API, queryString.getBytes(StandardCharsets.UTF_8));
request.setData(queryString.getBytes(StandardCharsets.UTF_8));
return request;
} }
public static HttpRequest loginTicketRequest(String service) { public static HttpRequest loginTicketRequest(String redirectUrl, String service) {
Map<String, String> requestForm = new HashMap<>(1); Map<String, String> requestForm = new HashMap<>(1);
requestForm.put("service", service); requestForm.put("service", service);
String queryString = StringUtil.generateQueryString(requestForm); String queryString = StringUtil.generateQueryString(requestForm);
HttpRequest request = new HttpRequest(); return makeHttpRequest(redirectUrl, queryString.getBytes(StandardCharsets.UTF_8));
request.setData(queryString.getBytes(StandardCharsets.UTF_8));
return request;
} }
} }

@ -1,9 +1,7 @@
package cn.linghang.mywust.core.service.auth; package cn.linghang.mywust.core.service.auth;
import cn.linghang.mywust.core.api.UnionAuth;
import cn.linghang.mywust.core.exception.BasicException; import cn.linghang.mywust.core.exception.BasicException;
import cn.linghang.mywust.core.exception.PasswordWornException; import cn.linghang.mywust.core.exception.PasswordWornException;
import cn.linghang.mywust.core.service.undergraduate.BkjxAuthRequestFactory;
import cn.linghang.mywust.network.HttpRequest; import cn.linghang.mywust.network.HttpRequest;
import cn.linghang.mywust.network.HttpResponse; import cn.linghang.mywust.network.HttpResponse;
import cn.linghang.mywust.network.RequestClientOption; import cn.linghang.mywust.network.RequestClientOption;
@ -13,7 +11,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
/** /**
* 统一认证登录 * 统一认证登录
@ -32,8 +29,7 @@ public class UnionLogin {
// 获取ticket granting ticket(TGT),以获取ServiceTicket // 获取ticket granting ticket(TGT),以获取ServiceTicket
HttpRequest TGTRequest = AuthRequestFactory.unionLoginTGTRequest(username, encodedPassword, serviceUrl); HttpRequest TGTRequest = AuthRequestFactory.unionLoginTGTRequest(username, encodedPassword, serviceUrl);
HttpResponse unionAuthResponse = requester.post(TGTRequest, requestOption);
HttpResponse unionAuthResponse = requester.post(new URL(UnionAuth.UNION_AUTH_API), TGTRequest, requestOption);
String redirectAddress = unionAuthResponse.getHeaders().get("Location"); String redirectAddress = unionAuthResponse.getHeaders().get("Location");
if (redirectAddress == null) { if (redirectAddress == null) {
@ -41,10 +37,8 @@ public class UnionLogin {
} }
// 获取服务ticket(service ticket,ST) // 获取服务ticket(service ticket,ST)
HttpRequest serviceTicketRequest = AuthRequestFactory.loginTicketRequest(serviceUrl); HttpRequest serviceTicketRequest = AuthRequestFactory.loginTicketRequest(redirectAddress.replace("http:", "https:"), serviceUrl);
HttpResponse serviceTicketResponse = requester.post(serviceTicketRequest, requestOption);
HttpResponse serviceTicketResponse =
requester.post(new URL(redirectAddress.replace("http:", "https:")), serviceTicketRequest, requestOption);
byte[] serviceTicketResponseData = serviceTicketResponse.getBody(); byte[] serviceTicketResponseData = serviceTicketResponse.getBody();
if (serviceTicketResponseData == null) { if (serviceTicketResponseData == null) {

@ -1,14 +1,15 @@
package cn.linghang.mywust.core.service.library; package cn.linghang.mywust.core.service.library;
import cn.linghang.mywust.core.api.Library;
import cn.linghang.mywust.network.HttpRequest; import cn.linghang.mywust.network.HttpRequest;
import cn.linghang.mywust.network.RequestFactory; import cn.linghang.mywust.network.RequestFactory;
public class LibraryAuthRequestFactory extends RequestFactory { public class LibraryAuthRequestFactory extends RequestFactory {
public static HttpRequest sessionCookieRequest() { public static HttpRequest sessionCookieRequest(String serviceTicket) {
return DEFAULT_HTTP_REQUEST; return makeHttpRequest(String.format(Library.LIBRARY_SESSION_COOKIE_API, serviceTicket));
} }
public static HttpRequest indexRequest() { public static HttpRequest indexRequest() {
return DEFAULT_HTTP_REQUEST; return makeHttpRequest(Library.LIBRARY_INDEX_URL);
} }
} }

@ -1,19 +1,15 @@
package cn.linghang.mywust.core.service.library; package cn.linghang.mywust.core.service.library;
import cn.linghang.mywust.core.api.Bkjx;
import cn.linghang.mywust.core.api.Library; import cn.linghang.mywust.core.api.Library;
import cn.linghang.mywust.core.api.UnionAuth; import cn.linghang.mywust.core.api.UnionAuth;
import cn.linghang.mywust.core.exception.BasicException; import cn.linghang.mywust.core.exception.BasicException;
import cn.linghang.mywust.core.service.auth.UnionLogin; import cn.linghang.mywust.core.service.auth.UnionLogin;
import cn.linghang.mywust.core.service.undergraduate.BkjxAuthRequestFactory;
import cn.linghang.mywust.network.HttpRequest; import cn.linghang.mywust.network.HttpRequest;
import cn.linghang.mywust.network.HttpResponse; import cn.linghang.mywust.network.HttpResponse;
import cn.linghang.mywust.network.RequestClientOption; import cn.linghang.mywust.network.RequestClientOption;
import cn.linghang.mywust.network.Requester; import cn.linghang.mywust.network.Requester;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public class LibraryLogin { public class LibraryLogin {
private final Requester requester; private final Requester requester;
@ -29,14 +25,14 @@ public class LibraryLogin {
String serviceTicket = unionLogin.getServiceTicket(username, password, UnionAuth.LIBRARY_SSO_SERVICE, requestOption); String serviceTicket = unionLogin.getServiceTicket(username, password, UnionAuth.LIBRARY_SSO_SERVICE, requestOption);
// 获取登录cookie(session) // 获取登录cookie(session)
HttpRequest sessionRequest = LibraryAuthRequestFactory.sessionCookieRequest(); HttpRequest sessionRequest = LibraryAuthRequestFactory.sessionCookieRequest(serviceTicket);
HttpResponse sessionResponse = requester.get(new URL(String.format(Library.LIBRARY_SESSION_COOKIE_API, serviceTicket)), sessionRequest, requestOption); HttpResponse sessionResponse = requester.get(sessionRequest, requestOption);
String cookies = sessionResponse.getCookies(); String cookies = sessionResponse.getCookies();
// 请求一次首页 // 请求一次首页
HttpRequest indexRequest = LibraryAuthRequestFactory.indexRequest(); HttpRequest indexRequest = LibraryAuthRequestFactory.indexRequest();
indexRequest.setCookies(cookies); indexRequest.setCookies(cookies);
requester.get(new URL(Library.LIBRARY_INDEX_URL), indexRequest, requestOption); requester.get(indexRequest, requestOption);
return cookies; return cookies;
} }
@ -44,9 +40,9 @@ public class LibraryLogin {
public boolean checkCookie(String cookies) throws IOException { public boolean checkCookie(String cookies) throws IOException {
RequestClientOption option = RequestClientOption.DEFAULT_OPTION; RequestClientOption option = RequestClientOption.DEFAULT_OPTION;
HttpRequest testRequest = LibraryAuthRequestFactory.getDefaultHttpRequest(); HttpRequest testRequest = LibraryAuthRequestFactory.makeHttpRequest(Library.LIBRARY_COOKIE_TEST_URL);
testRequest.setCookies(cookies); testRequest.setCookies(cookies);
HttpResponse testResponse = requester.get(new URL(Library.LIBRARY_COOKIE_TEST_URL), testRequest, option); HttpResponse testResponse = requester.get(testRequest, option);
// 响应居然是JSON,好评! // 响应居然是JSON,好评!
// 但是我们只要看有没有Unauthorized关键字就行了 // 但是我们只要看有没有Unauthorized关键字就行了

@ -1,5 +1,6 @@
package cn.linghang.mywust.core.service.undergraduate; package cn.linghang.mywust.core.service.undergraduate;
import cn.linghang.mywust.core.api.Bkjx;
import cn.linghang.mywust.network.HttpRequest; import cn.linghang.mywust.network.HttpRequest;
import cn.linghang.mywust.network.RequestFactory; import cn.linghang.mywust.network.RequestFactory;
import cn.linghang.mywust.util.StringUtil; import cn.linghang.mywust.util.StringUtil;
@ -9,17 +10,13 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class BkjxAuthRequestFactory extends RequestFactory { public class BkjxAuthRequestFactory extends RequestFactory {
public static HttpRequest sessionCookieRequest() { public static HttpRequest sessionCookieRequest(String serviceTicket) {
return DEFAULT_HTTP_REQUEST; return makeHttpRequest(String.format(Bkjx.BKJX_SESSION_COOKIE_API, serviceTicket));
}
public static HttpRequest getDefaultHttpRequest() {
return DEFAULT_HTTP_REQUEST;
} }
public static class Legacy { public static class Legacy {
public static HttpRequest dataStringRequest() { public static HttpRequest dataStringRequest() {
return DEFAULT_HTTP_REQUEST; return makeHttpRequest(Bkjx.Legacy.BKJX_DATA_STRING_API);
} }
public static HttpRequest ticketRedirectRequest(String encode) { public static HttpRequest ticketRedirectRequest(String encode) {
@ -34,11 +31,8 @@ public class BkjxAuthRequestFactory extends RequestFactory {
extendHeaders.put("Referer", "http://bkjx.wust.edu.cn/"); extendHeaders.put("Referer", "http://bkjx.wust.edu.cn/");
extendHeaders.put("Origin", "http://bkjx.wust.edu.cn"); extendHeaders.put("Origin", "http://bkjx.wust.edu.cn");
HttpRequest httpRequest = new HttpRequest(); return makeHttpRequest(Bkjx.Legacy.BKJX_SESSION_COOKIE_API, queryString.getBytes(StandardCharsets.UTF_8))
httpRequest.setHeaders(extendHeaders); .addHeaders(extendHeaders);
httpRequest.setData(queryString.getBytes(StandardCharsets.UTF_8));
return httpRequest;
} }
} }
} }

@ -14,7 +14,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
public class JwcLogin { public class JwcLogin {
private static final Logger log = LoggerFactory.getLogger(JwcLogin.class); private static final Logger log = LoggerFactory.getLogger(JwcLogin.class);
@ -29,7 +28,7 @@ public class JwcLogin {
} }
/** /**
* <p>旧版的登录方式既相当于直接登录<a href="http://bkjx.wust.edu.cn">bkjx系统不建议使用</a></p> * <p>旧版的登录方式既相当于直接登录<a href="http://bkjx.wust.edu.cn">bkjx系统</a>不建议使用</p>
* <p>注意这种登录方式的密码和新版可能是不一样的</p> * <p>注意这种登录方式的密码和新版可能是不一样的</p>
* <p>不过不论使用哪种登录方式获取到的cookie都是可用的</p> * <p>不过不论使用哪种登录方式获取到的cookie都是可用的</p>
* *
@ -39,7 +38,7 @@ public class JwcLogin {
public String getLoginCookieLegacy(String username, String password, RequestClientOption requestOption) throws IOException, BasicException { public String getLoginCookieLegacy(String username, String password, RequestClientOption requestOption) throws IOException, BasicException {
// 获取某段神秘的dataStr(反正官网代码是这么叫的) // 获取某段神秘的dataStr(反正官网代码是这么叫的)
HttpRequest dataStringRequest = BkjxAuthRequestFactory.Legacy.dataStringRequest(); HttpRequest dataStringRequest = BkjxAuthRequestFactory.Legacy.dataStringRequest();
HttpResponse dataStringResponse = requester.post(new URL(Bkjx.Legacy.BKJX_DATA_STRING_API), dataStringRequest, requestOption); HttpResponse dataStringResponse = requester.post(dataStringRequest, requestOption);
if (dataStringResponse.getBody() == null) { if (dataStringResponse.getBody() == null) {
throw new BasicException(); throw new BasicException();
} }
@ -50,7 +49,8 @@ public class JwcLogin {
String encoded = PasswordEncoder.legacyPassword(username, password, dataString); String encoded = PasswordEncoder.legacyPassword(username, password, dataString);
HttpRequest ticketRequest = BkjxAuthRequestFactory.Legacy.ticketRedirectRequest(encoded); HttpRequest ticketRequest = BkjxAuthRequestFactory.Legacy.ticketRedirectRequest(encoded);
ticketRequest.setCookies(dataStringResponse.getCookies()); ticketRequest.setCookies(dataStringResponse.getCookies());
HttpResponse ticketResponse = requester.post(new URL(Bkjx.Legacy.BKJX_SESSION_COOKIE_API), ticketRequest, requestOption);
HttpResponse ticketResponse = requester.post(ticketRequest, requestOption);
if (ticketResponse.getBody() == null) { if (ticketResponse.getBody() == null) {
throw new BasicException(); throw new BasicException();
} }
@ -60,9 +60,9 @@ public class JwcLogin {
if (sessionRedirect == null) { if (sessionRedirect == null) {
throw new PasswordWornException(); throw new PasswordWornException();
} }
HttpRequest sessionRequest = BkjxAuthRequestFactory.Legacy.dataStringRequest();
HttpResponse sessionResponse = requester.get(new URL(sessionRedirect), sessionRequest, requestOption); HttpRequest sessionRequest = BkjxAuthRequestFactory.makeHttpRequest(sessionRedirect);
HttpResponse sessionResponse = requester.get(sessionRequest, requestOption);
String cookies = sessionResponse.getCookies(); String cookies = sessionResponse.getCookies();
if (roughCheckCookie(cookies)) { if (roughCheckCookie(cookies)) {
@ -76,8 +76,8 @@ public class JwcLogin {
String serviceTicket = unionLogin.getServiceTicket(username, password, UnionAuth.BKJX_SSO_SERVICE, requestOption); String serviceTicket = unionLogin.getServiceTicket(username, password, UnionAuth.BKJX_SSO_SERVICE, requestOption);
// 获取登录cookie(session) // 获取登录cookie(session)
HttpRequest sessionRequest = BkjxAuthRequestFactory.sessionCookieRequest(); HttpRequest sessionRequest = BkjxAuthRequestFactory.sessionCookieRequest(serviceTicket);
HttpResponse sessionResponse = requester.get(new URL(String.format(Bkjx.BKJX_SESSION_COOKIE_API, serviceTicket)), sessionRequest, requestOption); HttpResponse sessionResponse = requester.get(sessionRequest, requestOption);
String cookies = sessionResponse.getCookies(); String cookies = sessionResponse.getCookies();
if (roughCheckCookie(cookies)) { if (roughCheckCookie(cookies)) {
@ -87,7 +87,7 @@ public class JwcLogin {
return cookies; return cookies;
} }
private boolean roughCheckCookie(String cookies) throws BasicException { private boolean roughCheckCookie(String cookies) {
return cookies == null || !cookies.contains("JSESSIONID") || !cookies.contains("SERVERID"); return cookies == null || !cookies.contains("JSESSIONID") || !cookies.contains("SERVERID");
} }
@ -96,13 +96,9 @@ public class JwcLogin {
"'https://auth.wust.edu.cn/lyuapServer/login?service=http%3A%2F%2Fbkjx.wust.edu.cn%2Fjsxsd%2Fxxwcqk%2Fxxwcqk_idxOnzh.do'" + "'https://auth.wust.edu.cn/lyuapServer/login?service=http%3A%2F%2Fbkjx.wust.edu.cn%2Fjsxsd%2Fxxwcqk%2Fxxwcqk_idxOnzh.do'" +
"</script>").length(); "</script>").length();
public boolean checkCookies(String cookies, RequestClientOption option) throws IOException {
public boolean checkCookies(String cookies) throws IOException { HttpRequest testRequest = BkjxAuthRequestFactory.makeHttpRequest(Bkjx.BKJX_TEST_API, null, cookies);
RequestClientOption option = RequestClientOption.DEFAULT_OPTION; HttpResponse testResponse = requester.get(testRequest, option);
HttpRequest testRequest = BkjxAuthRequestFactory.getDefaultHttpRequest();
testRequest.setCookies(cookies);
HttpResponse testResponse = requester.get(new URL(Bkjx.BKJX_TEST_API), testRequest, option);
// 判断响应长度是否为178个字,登录跳转响应长度 // 判断响应长度是否为178个字,登录跳转响应长度
// 这种办法虽然在极端情况下可能会出错(而且还挺蠢的),但是是最快的办法中比较准确的了 // 这种办法虽然在极端情况下可能会出错(而且还挺蠢的),但是是最快的办法中比较准确的了

@ -13,9 +13,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Proxy; import java.net.Proxy;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
@ -186,12 +184,11 @@ public class SimpleOkhttpRequester implements Requester {
* 根据请求信息来构建Request * 根据请求信息来构建Request
* *
* @param requestMethod 请求方法可选值GET, POST, PUT, DELETE * @param requestMethod 请求方法可选值GET, POST, PUT, DELETE
* @param url 地址
* @param httpRequest 请求体 * @param httpRequest 请求体
* @return Request * @return Request
*/ */
private Request buildRequest(RequestMethod requestMethod, URL url, HttpRequest httpRequest) { private Request buildRequest(RequestMethod requestMethod, HttpRequest httpRequest) {
Request.Builder requestBuilder = new Request.Builder().url(url); Request.Builder requestBuilder = new Request.Builder().url(httpRequest.getUrl());
Map<String, String> requestHeaders = httpRequest.getHeaders(); Map<String, String> requestHeaders = httpRequest.getHeaders();
if (requestHeaders != null) { if (requestHeaders != null) {
@ -268,15 +265,14 @@ public class SimpleOkhttpRequester implements Requester {
* 执行请求 * 执行请求
* *
* @param requestMethod 请求方法 * @param requestMethod 请求方法
* @param url 请求地址
* @param httpRequest 请求体 * @param httpRequest 请求体
* @param requestClientOption 请求响应 * @param requestClientOption 请求响应
* @return 包装好的Http响应 * @return 包装好的Http响应
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
private HttpResponse doRequest(RequestMethod requestMethod, URL url, HttpRequest httpRequest, RequestClientOption requestClientOption) throws IOException { private HttpResponse doRequest(RequestMethod requestMethod, HttpRequest httpRequest, RequestClientOption requestClientOption) throws IOException {
OkHttpClient client = getOkhttpClient(requestClientOption); OkHttpClient client = getOkhttpClient(requestClientOption);
Request request = this.buildRequest(requestMethod, url, httpRequest); Request request = this.buildRequest(requestMethod, httpRequest);
log.debug("------------Do Request------------"); log.debug("------------Do Request------------");
log.debug("Request Options: {}" ,requestClientOption); log.debug("Request Options: {}" ,requestClientOption);
@ -287,31 +283,31 @@ public class SimpleOkhttpRequester implements Requester {
} }
@Override @Override
public HttpResponse get(URL url, HttpRequest httpRequest, RequestClientOption requestClientOption) throws IOException { public HttpResponse get(HttpRequest httpRequest, RequestClientOption requestClientOption) throws IOException {
return this.doRequest(RequestMethod.GET, url, httpRequest, requestClientOption); return this.doRequest(RequestMethod.GET, httpRequest, requestClientOption);
} }
@Override @Override
public HttpResponse post(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException { public HttpResponse post(HttpRequest request, RequestClientOption requestClientOption) throws IOException {
return this.doRequest(RequestMethod.POST, url, request, requestClientOption); return this.doRequest(RequestMethod.POST, request, requestClientOption);
} }
@Override @Override
public HttpResponse postJson(URL url, HttpRequest request, Object requestBody, RequestClientOption requestClientOption) throws IOException { public HttpResponse postJson(HttpRequest request, Object requestBody, RequestClientOption requestClientOption) throws IOException {
String json = new ObjectMapper().writeValueAsString(requestBody); String json = new ObjectMapper().writeValueAsString(requestBody);
request.setData(json.getBytes(StandardCharsets.UTF_8)); request.setData(json.getBytes(StandardCharsets.UTF_8));
request.getHeaders().put("Content-Type", JSON_CONTENT_TYPE); request.getHeaders().put("Content-Type", JSON_CONTENT_TYPE);
return this.doRequest(RequestMethod.POST, url, request, requestClientOption); return this.doRequest(RequestMethod.POST, request, requestClientOption);
} }
@Override @Override
public HttpResponse put(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException { public HttpResponse put(HttpRequest request, RequestClientOption requestClientOption) throws IOException {
return this.doRequest(RequestMethod.PUT, url, request, requestClientOption); return this.doRequest(RequestMethod.PUT, request, requestClientOption);
} }
@Override @Override
public HttpResponse delete(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException { public HttpResponse delete(HttpRequest request, RequestClientOption requestClientOption) throws IOException {
return this.doRequest(RequestMethod.DELETE, url, request, requestClientOption); return this.doRequest(RequestMethod.DELETE, request, requestClientOption);
} }
} }

@ -2,6 +2,7 @@ package cn.linghang.mywust.network;
import lombok.Data; import lombok.Data;
import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -20,7 +21,7 @@ public class HttpRequest {
return defaultHeaders; return defaultHeaders;
} }
private Map<String, String> urlParams; private URL url;
private Map<String, String> headers; private Map<String, String> headers;
@ -32,14 +33,15 @@ public class HttpRequest {
headers = new HashMap<>(DEFAULT_HEADERS); headers = new HashMap<>(DEFAULT_HEADERS);
} }
public void setHeaders(Map<String, String> headers) { public HttpRequest addHeaders(Map<String, String> headers) {
this.headers.putAll(headers); this.headers.putAll(headers);
return this;
} }
@Override @Override
public String toString() { public String toString() {
final StringBuffer sb = new StringBuffer("HttpRequest{"); final StringBuffer sb = new StringBuffer("HttpRequest{");
sb.append("urlParams=").append(urlParams); sb.append("urlParams=").append(url);
sb.append(", headers=").append(headers); sb.append(", headers=").append(headers);
sb.append(", cookies='").append(cookies).append('\''); sb.append(", cookies='").append(cookies).append('\'');
sb.append(", data="); sb.append(", data=");
@ -50,4 +52,44 @@ public class HttpRequest {
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }
public static HttpRequestBuilder builder() {
return new HttpRequestBuilder();
}
public static class HttpRequestBuilder {
private final HttpRequest httpRequest;
private HttpRequestBuilder() {
httpRequest = new HttpRequest();
}
public static HttpRequestBuilder aHttpRequest() {
return new HttpRequestBuilder();
}
public HttpRequestBuilder url(URL url) {
httpRequest.setUrl(url);
return this;
}
public HttpRequestBuilder headers(Map<String, String> headers) {
httpRequest.setHeaders(headers);
return this;
}
public HttpRequestBuilder cookies(String cookies) {
httpRequest.setCookies(cookies);
return this;
}
public HttpRequestBuilder data(byte[] data) {
httpRequest.setData(data);
return this;
}
public HttpRequest build() {
return httpRequest;
}
}
} }

@ -25,4 +25,39 @@ public class HttpResponse {
sb.append('}'); sb.append('}');
return sb.toString(); return sb.toString();
} }
public static HttpResponseBuilder builder() {
return new HttpResponseBuilder();
}
public static final class HttpResponseBuilder {
private final HttpResponse httpResponse;
private HttpResponseBuilder() {
httpResponse = new HttpResponse();
}
public static HttpResponseBuilder aHttpResponse() {
return new HttpResponseBuilder();
}
public HttpResponseBuilder headers(Map<String, String> headers) {
httpResponse.setHeaders(headers);
return this;
}
public HttpResponseBuilder cookies(String cookies) {
httpResponse.setCookies(cookies);
return this;
}
public HttpResponseBuilder body(byte[] body) {
httpResponse.setBody(body);
return this;
}
public HttpResponse build() {
return httpResponse;
}
}
} }

@ -1,9 +1,32 @@
package cn.linghang.mywust.network; package cn.linghang.mywust.network;
import java.net.URL;
public class RequestFactory { public class RequestFactory {
protected static final HttpRequest DEFAULT_HTTP_REQUEST = new HttpRequest(); protected static final HttpRequest DEFAULT_HTTP_REQUEST = new HttpRequest();
public static HttpRequest getDefaultHttpRequest() { public static HttpRequest makeHttpRequest(String url) {
return DEFAULT_HTTP_REQUEST; return makeHttpRequest(url, null, null);
}
public static HttpRequest makeHttpRequest(String url, byte[] data) {
return makeHttpRequest(url, data, null);
}
public static HttpRequest makeHttpRequest(String url, byte[] data, String cookies) {
return HttpRequest.builder()
.url(makeUrl(url))
.data(data)
.cookies(cookies)
.build()
.addHeaders(DEFAULT_HTTP_REQUEST.getHeaders());
}
public static URL makeUrl(String url) {
try {
return new URL(url);
} catch (Exception e) {
return null;
}
} }
} }

@ -1,7 +1,6 @@
package cn.linghang.mywust.network; package cn.linghang.mywust.network;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
/** /**
* <p>请求类的上层封装接口传入参数直接进行请求</p> * <p>请求类的上层封装接口传入参数直接进行请求</p>
@ -18,55 +17,51 @@ public interface Requester {
/** /**
* 发送Get请求 * 发送Get请求
* *
* @param url 请求地址
* @param request 请求体 * @param request 请求体
* @param requestClientOption 请求选项 * @param requestClientOption 请求选项
* @return 响应数据 * @return 响应数据
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
HttpResponse get(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException; HttpResponse get(HttpRequest request, RequestClientOption requestClientOption) throws IOException;
/** /**
* 发送Post请求 * 发送Post请求
* *
* @param url 请求地址
* @param request 请求体 * @param request 请求体
* @param requestClientOption 请求选项 * @param requestClientOption 请求选项
* @return 响应数据 * @return 响应数据
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
HttpResponse post(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException; HttpResponse post(HttpRequest request, RequestClientOption requestClientOption) throws IOException;
/** /**
* 发送Post请求并将对象自动封装成json * 发送Post请求并将对象自动封装成json
* *
* @param url 请求地址
* @param request 请求体 * @param request 请求体
* @param requestClientOption 请求选项 * @param requestClientOption 请求选项
* @return 响应数据 * @return 响应数据
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
HttpResponse postJson(URL url, HttpRequest request, Object requestBody, RequestClientOption requestClientOption) throws IOException; HttpResponse postJson(HttpRequest request, Object requestBody, RequestClientOption requestClientOption) throws IOException;
/** /**
* 发送Put请求 * 发送Put请求
* *
* @param url 请求地址
* @param request 请求体 * @param request 请求体
* @param requestClientOption 请求选项 * @param requestClientOption 请求选项
* @return 响应数据 * @return 响应数据
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
HttpResponse put(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException; HttpResponse put(HttpRequest request, RequestClientOption requestClientOption) throws IOException;
/** /**
* 发送Delete请求 * 发送Delete请求
* *
* @param url 请求地址
* @param request 请求体 * @param request 请求体
* @param requestClientOption 请求选项 * @param requestClientOption 请求选项
* @return 响应数据 * @return 响应数据
* @throws IOException 如果网络请求有异常 * @throws IOException 如果网络请求有异常
*/ */
HttpResponse delete(URL url, HttpRequest request, RequestClientOption requestClientOption) throws IOException; HttpResponse delete(HttpRequest request, RequestClientOption requestClientOption) throws IOException;
} }

@ -39,6 +39,6 @@ public class JwcLoginTest {
System.out.printf("获取到Cookies: %s \n", cookies); System.out.printf("获取到Cookies: %s \n", cookies);
System.out.printf("检查Cookies: %s", jwcLogin.checkCookies(cookies)); System.out.printf("检查Cookies: %s", jwcLogin.checkCookies(cookies, RequestClientOption.DEFAULT_OPTION));
} }
} }

Loading…
Cancel
Save