fix http bug

This commit is contained in:
Looly
2020-09-05 20:07:06 +08:00
parent cbbf4671ba
commit 3e05c5b396
7 changed files with 85 additions and 15 deletions

View File

@@ -444,9 +444,21 @@ public class HttpConnection {
throw new IOException("HttpURLConnection has not been initialized.");
}
final Method method = getMethod();
// 当有写出需求时,自动打开之
this.conn.setDoOutput(true);
return this.conn.getOutputStream();
final OutputStream out = this.conn.getOutputStream();
// 解决在Rest请求中GET请求附带body导致GET请求被强制转换为POST
// 在sun.net.www.protocol.http.HttpURLConnection.getOutputStream0方法中会把GET方法
// 修改为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());
}
return out;
}
/**