<bdo id='gTdP5'></bdo><ul id='gTdP5'></ul>

  • <legend id='gTdP5'><style id='gTdP5'><dir id='gTdP5'><q id='gTdP5'></q></dir></style></legend>

    <small id='gTdP5'></small><noframes id='gTdP5'>

      <i id='gTdP5'><tr id='gTdP5'><dt id='gTdP5'><q id='gTdP5'><span id='gTdP5'><b id='gTdP5'><form id='gTdP5'><ins id='gTdP5'></ins><ul id='gTdP5'></ul><sub id='gTdP5'></sub></form><legend id='gTdP5'></legend><bdo id='gTdP5'><pre id='gTdP5'><center id='gTdP5'></center></pre></bdo></b><th id='gTdP5'></th></span></q></dt></tr></i><div id='gTdP5'><tfoot id='gTdP5'></tfoot><dl id='gTdP5'><fieldset id='gTdP5'></fieldset></dl></div>
      <tfoot id='gTdP5'></tfoot>

        发送带有标头的 HTTP GET 请求

        Send HTTP GET request with header(发送带有标头的 HTTP GET 请求)

          <tfoot id='xPpc8'></tfoot>
            <tbody id='xPpc8'></tbody>
            <legend id='xPpc8'><style id='xPpc8'><dir id='xPpc8'><q id='xPpc8'></q></dir></style></legend>
          • <i id='xPpc8'><tr id='xPpc8'><dt id='xPpc8'><q id='xPpc8'><span id='xPpc8'><b id='xPpc8'><form id='xPpc8'><ins id='xPpc8'></ins><ul id='xPpc8'></ul><sub id='xPpc8'></sub></form><legend id='xPpc8'></legend><bdo id='xPpc8'><pre id='xPpc8'><center id='xPpc8'></center></pre></bdo></b><th id='xPpc8'></th></span></q></dt></tr></i><div id='xPpc8'><tfoot id='xPpc8'></tfoot><dl id='xPpc8'><fieldset id='xPpc8'></fieldset></dl></div>

              <small id='xPpc8'></small><noframes id='xPpc8'>

                  <bdo id='xPpc8'></bdo><ul id='xPpc8'></ul>

                  本文介绍了发送带有标头的 HTTP GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  From my Android app I want to request a URL with GET parameters and read the response. In the request I must add a x-zip header.

                  网址类似于

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

                  谁能提供我的代码?

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

                  Two things are important: that it is a GET request and contains the x-zip header .

                  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 标头,我的代码中的标头可以吗?

                  I try with this code but I get code with .net error: Object reference not set to an instance of an object... I think but I'm not sure this if for x-zip header, is header in my code ok?

                  推荐答案

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

                  Here's a code excerpt we're using in our app to set request headers. You'll note we set the CONTENT_TYPE header only on a POST or PUT, but the general method of adding headers (via a request interceptor) is used for GET as well.

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

                  这篇关于发送带有标头的 HTTP GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                    <bdo id='wqYEu'></bdo><ul id='wqYEu'></ul>
                  • <small id='wqYEu'></small><noframes id='wqYEu'>

                    <legend id='wqYEu'><style id='wqYEu'><dir id='wqYEu'><q id='wqYEu'></q></dir></style></legend><tfoot id='wqYEu'></tfoot>

                      <i id='wqYEu'><tr id='wqYEu'><dt id='wqYEu'><q id='wqYEu'><span id='wqYEu'><b id='wqYEu'><form id='wqYEu'><ins id='wqYEu'></ins><ul id='wqYEu'></ul><sub id='wqYEu'></sub></form><legend id='wqYEu'></legend><bdo id='wqYEu'><pre id='wqYEu'><center id='wqYEu'></center></pre></bdo></b><th id='wqYEu'></th></span></q></dt></tr></i><div id='wqYEu'><tfoot id='wqYEu'></tfoot><dl id='wqYEu'><fieldset id='wqYEu'></fieldset></dl></div>
                        <tbody id='wqYEu'></tbody>