diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd85ffbb..f09f29030 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### 🐣新特性 ### 🐞Bug修复 +* 【http 】 修复https下可能的Patch、Get请求失效问题(issue#I3Z3DH@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java b/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java index 44114a3ed..a2c23c419 100644 --- a/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java +++ b/hutool-http/src/main/java/cn/hutool/http/HttpConnection.java @@ -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 }