【问题标题】:send header value using HttpUrlConnection使用 HttpUrlConnection 发送标头值
【发布时间】:2017-09-05 07:28:15
【问题描述】:

我试图使用身份验证值向服务器发送响应,它在登录时为我提供。服务器仅在发送该值时接受请求。但是它适用于postman (see snapshot),但不适用于我的代码。

代码

 URL url = new URL(strings[0]);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            String token = "" + new String(accessToken);
            con.setRequestMethod("GET");
            con.setRequestProperty("AuthToken", token);
            con.setReadTimeout(15000);
            con.setConnectTimeout(15000);
            con.setDoOutput(false);

我也试过这种方式

String token = "AuthToken :" + new String(accessToken);
con.setRequestProperty("Authorization", token);

【问题讨论】:

标签: android get httpurlconnection http-token-authentication


【解决方案1】:

试试这个

URL url = new URL(strings[0]);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        String token = "" + new String(accessToken);
        con.setRequestMethod("GET");
        con.setRequestProperty("AuthToken", token);
        con.setReadTimeout(15000);
        con.setConnectTimeout(15000);
        con.setDoOutput(false);

【讨论】:

    【解决方案2】:

    使用这个 -

    **
     * HTTP request types
     */
    public static final int POST_TYPE   = 1;
    public static final int GET_TYPE    = 2;
    public static final int PUT_TYPE    = 3;
    public static final int DELETE_TYPE = 4;
    
    /**
     * HTTP request header constants
     */
    public static final String CONTENT_TYPE         = "Content-Type";
    public static final String ACCEPT_ENCODING      = "Accept-Encoding";
    public static final String CONTENT_ENCODING     = "Content-Encoding";
    public static final String ENCODING_GZIP        = "gzip";
    public static final String MIME_FORM_ENCODED    = "application/x-www-form-urlencoded";
    public static final String MIME_TEXT_PLAIN      = "text/plain";
    
    private InputStream performRequest(final String contentType, final String url, final String user, final String pass,
        final Map<String, String> headers, final Map<String, String> params, final int requestType) 
                throws IOException {
    
        DefaultHttpClient client = HTTPClientFactory.newClient();
    
        client.getParams().setParameter(HttpProtocolParams.USER_AGENT, mUserAgent);
    
        // add user and pass to client credentials if present
        if ((user != null) && (pass != null)) {
            client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
        }
    
        // process headers using request interceptor
        final Map<String, String> sendHeaders = new HashMap<String, String>();
        if ((headers != null) && (headers.size() > 0)) {
            sendHeaders.putAll(headers);
        }
        if (requestType == HTTPRequestHelper.POST_TYPE || requestType == HTTPRequestHelper.PUT_TYPE ) {
            sendHeaders.put(HTTPRequestHelper.CONTENT_TYPE, contentType);
        }
        // request gzip encoding for response
        sendHeaders.put(HTTPRequestHelper.ACCEPT_ENCODING, HTTPRequestHelper.ENCODING_GZIP);
    
        if (sendHeaders.size() > 0) {
            client.addRequestInterceptor(new HttpRequestInterceptor() {
    
                public void process(final HttpRequest request, final HttpContext context) throws HttpException,
                    IOException {
                    for (String key : sendHeaders.keySet()) {
                        if (!request.containsHeader(key)) {
                            request.addHeader(key, sendHeaders.get(key));
                        }
                    }
                }
            });
        }
    
        //.... code omitted ....//
    
    }
    

    参考 - Send HTTP GET request with header

    【讨论】:

    • 我想用 httpUrlConnection 来做这个
    【解决方案3】:

    试试这个方法..

    String token = new String(new Base64().encode(accessToken.getBytes()));
    con.setRequestProperty("AuthToken", token);
    

    【讨论】:

      【解决方案4】:

      尝试替换 myURLConnection.setRequestMethod("GET"); with myURLConnection.setRequestProperty("Request Method", "GET");

      请在下面找到代码:

      URL myURL = new URL(serviceURL);
      HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
      String userCredentials = "username:password";
      String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
      myURLConnection.setRequestProperty ("Authorization", basicAuth);
      myURLConnection.setRequestMethod("POST");
      myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
      myURLConnection.setRequestProperty("Content-Language", "en-US");
      myURLConnection.setUseCaches(false);
      myURLConnection.setDoInput(true);
      myURLConnection.setDoOutput(true);
      

      【讨论】:

      • 我需要使用get方法来做这个
      • 你可以用 get 做同样的事情
      猜你喜欢
      • 1970-01-01
      • 2018-10-09
      • 2019-01-30
      • 2018-12-17
      • 2018-03-17
      • 1970-01-01
      • 2014-09-20
      • 2023-04-09
      • 2012-01-09
      相关资源
      最近更新 更多