【问题标题】:Send HTTP GET request with header发送带有标头的 HTTP GET 请求
【发布时间】:2011-09-20 12:44:02
【问题描述】:

我想从我的 Android 应用中请求一个带有 GET 参数的 URL 并读取响应。 在请求中,我必须添加一个 x-zip 标头。

网址类似于

http://example.com/getmethod.aspx?id=111&method=Test

谁能提供我的代码?

有两点很重要:它是一个 GET 请求并包含 x-zip 标头。

编辑:

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://example.com/getmethod.aspx?id=111&method=Test";
    HttpGet get = new HttpGet(getURL);
    get.setHeader("Content-Type", "application/x-zip");
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
        //do something with the response
        Log.i("GET ",EntityUtils.toString(resEntityGet));
    }
} catch (Exception e) {
    e.printStackTrace();
}

我尝试使用此代码,但我得到带有 .net 错误的代码:Object reference not set to an instance of an object... 我想,但我不确定如果x-zip 标头,我的代码中的标头可以吗?

【问题讨论】:

  • 您能向我们展示一些您迄今为止尝试过的代码示例吗?
  • 你会发布实际的错误吗?这听起来像是 getmethod.aspx 的问题,而不是 java 代码的问题。
  • 为什么要发送带有 GET 请求的 Content-Type 标头字段???

标签: java android http http-headers


【解决方案1】:

你完全按照你在这行显示的那样做:

get.setHeader("Content-Type", "application/x-zip");

所以你的标题很好,问题是网络服务的一些其他输入。您需要在服务器端进行调试。

【讨论】:

  • get.setHeader("Accept-Encoding", "x-zip"); 改为 get.setHeader(Content-Type"", "application/x-zip");
  • @Jovan 啊,这更有意义。当然,你可以设置任何你喜欢的标题,它们没有任何意义...... :)
【解决方案2】:

这是我们在应用中用于设置请求标头的代码摘录。您会注意到我们仅在 POST 或 PUT 上设置 CONTENT_TYPE 标头,但添加标头的一般方法(通过请求拦截器)也用于 GET。

/**
 * 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 ....//

}

【讨论】:

  • 感谢您的回答,这部分是我错的地方sendHeaders.put(HTTPRequestHelper.ACCEPT_ENCODING, HTTPRequestHelper.ENCODING_GZIP);
  • addRequestInterceptor 那是什么.....看起来你正在拦截自己的请求! ?!?!?! :o
猜你喜欢
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多