HttpURLConnection c = URL.openConnection(); c.setRequestProperty() Doesn't work

杀马特。学长 韩版系。学妹 提交于 2019-11-30 10:51:33

Instead of setting Content-Length in the request property, use setFixedLengthStreamingMode(postData.length);

According to the source for HttpUrlConnection , Content-Length is a "restricted header":

146       /*
147        * Restrict setting of request headers through the public api
148        * consistent with JavaScript XMLHttpRequest2 with a few
149        * exceptions. Disallowed headers are silently ignored for
150        * backwards compatibility reasons rather than throwing a
151        * SecurityException. For example, some applets set the
152        * Host header since old JREs did not implement HTTP 1.1.
153        * Additionally, any header starting with Sec- is
154        * disallowed.
155        *
156        * The following headers are allowed for historical reasons:
157        *
158        * Accept-Charset, Accept-Encoding, Cookie, Cookie2, Date,
159        * Referer, TE, User-Agent, headers beginning with Proxy-.
160        *
161        * The following headers are allowed in a limited form:
162        *
163        * Connection: close
164        *
165        * See http://www.w3.org/TR/XMLHttpRequest2.
166        */
167       private static final boolean allowRestrictedHeaders;
168       private static final Set<String> restrictedHeaderSet;
169       private static final String[] restrictedHeaders = {
170           /* Restricted by XMLHttpRequest2 */
171           //"Accept-Charset",
172           //"Accept-Encoding",
173           "Access-Control-Request-Headers",
174           "Access-Control-Request-Method",
175           "Connection", /* close is allowed */
176           "Content-Length",
177           //"Cookie",
178           //"Cookie2",
179           "Content-Transfer-Encoding",
180           //"Date",
181           //"Expect",
182           "Host",
183           "Keep-Alive",
184           "Origin",
185           // "Referer",
186           // "TE",
187           "Trailer",
188           "Transfer-Encoding",
189           "Upgrade",
190           //"User-Agent",
191           "Via"
192       };

So, setting Content-Length will be silently ignored.

Authorization is blocked from being returned for security purposes:

249       // the following http request headers should NOT have their values
250       // returned for security reasons.
251       private static final String[] EXCLUDE_HEADERS = {
252               "Proxy-Authorization",
253               "Authorization"
254       };
255   
256       // also exclude system cookies when any might be set
257       private static final String[] EXCLUDE_HEADERS2= {
258               "Proxy-Authorization",
259               "Authorization",
260               "Cookie",
261               "Cookie2"
262       };

So even if you set the authorization header, you won't get it back when you query the headers.

The content length is set automatically. You can't set it yourself directly. You can however pass the correct length when setting fixed length streaming mode.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!