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

后端 未结 2 1665
灰色年华
灰色年华 2021-01-01 07:37

This Code here is a normal Java application not an android application, this is designed to send C2DM messages to a device with YOUR_REGISTRATION_STRING as the developer wit

相关标签:
2条回答
  • 2021-01-01 08:09

    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.

    0 讨论(0)
  • 2021-01-01 08:10

    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.

    0 讨论(0)
提交回复
热议问题