修复https下可能的Patch、Get请求失效问题

This commit is contained in:
Looly 2022-07-30 00:59:25 +08:00
parent 6bdd6924ea
commit f9610d523d
2 changed files with 22 additions and 3 deletions

View File

@ -7,6 +7,7 @@
### 🐣新特性
### 🐞Bug修复
* 【http 】 修复https下可能的Patch、Get请求失效问题issue#I3Z3DH@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -128,9 +128,13 @@ public class HttpConnection {
try {
this.conn.setRequestMethod(method.toString());
} catch (ProtocolException e) {
throw new HttpException(e);
if(Method.PATCH.equals(method)){
// 如果全局设置失效此处针对单独链接重新设置
reflectSetMethod(method);
}else{
throw new HttpException(e);
}
}
return this;
}
@ -453,7 +457,7 @@ public class HttpConnection {
// 修改为POST而且无法调用setRequestMethod方法修改因此此处使用反射强制修改字段属性值
// https://stackoverflow.com/questions/978061/http-get-with-request-body/983458
if(method == Method.GET && method != getMethod()){
ReflectUtil.setFieldValue(this.conn, "method", Method.GET.name());
reflectSetMethod(method);
}
return out;
@ -544,5 +548,19 @@ public class HttpConnection {
private URLConnection openConnection() throws IOException {
return (null == this.proxy) ? url.openConnection() : url.openConnection(this.proxy);
}
/**
* 通过反射设置方法名首先设置HttpURLConnection本身的方法名再检查是否为代理类如果是设置带路对象的方法名
* @param method 方法名
*/
private void reflectSetMethod(Method method){
ReflectUtil.setFieldValue(this.conn, "method", method.name());
// HttpsURLConnectionImpl实现中使用了代理类需要修改被代理类的method方法
final Object delegate = ReflectUtil.getFieldValue(this.conn, "delegate");
if(null != delegate){
ReflectUtil.setFieldValue(delegate, "method", method.name());
}
}
// --------------------------------------------------------------- Private Method end
}