コード例 #1
0
 public String toString() {
   String postData = mConn.getPostData() == null ? "null" : new String(mConn.getPostData());
   return "url: "
       + mConn.getRequestUrl()
       + "\nheaders: "
       + mConn.getRequestHeaders()
       + "\npostData: "
       + postData;
 }
コード例 #2
0
    public void run() {
      try {
        HttpUriRequest request = mConn.getHttpRequest();
        InetAddress ia = InetAddress.getByName(request.getURI().getHost());
        LogUtils.logd("Requesting: " + mConn.getRequestUrl() + " (" + ia.getHostAddress() + ")");

        request.addHeader("User-Agent", USER_AGENT);
        for (NameValuePair header : mConn.getRequestHeaders()) {
          request.addHeader(header.getName(), header.getValue());
        }

        LogUtils.logd(
            "Headers: " + Arrays.asList(mConn.getHttpRequest().getAllHeaders()).toString());
        if (mConn.getHttpRequest() instanceof HttpPost) {
          String postBody = EntityUtils.toString(((HttpPost) mConn.getHttpRequest()).getEntity());
          LogUtils.logd("POST to " + mConn.getRequestUrl() + ": " + postBody);
        }

        HttpResponse response;
        try {
          response = mHttpClient.execute(request);
        } catch (IOException e) {
          // XXX Mediocre way to match exceptions from aborted requests:
          if (request.isAborted() && e.getMessage().contains("abort")) {
            throw new AbortedRequestException();
          } else {
            throw e;
          }
        }

        if (request.isAborted()) throw new AbortedRequestException();

        // Fetching the status code allows the response interceptor to have a chance to un-gzip the
        // entity before we fetch it.
        response.getStatusLine().getStatusCode();

        HttpResponseHeaders headers = HttpResponseHeaders.fromResponse(response, request);

        HttpEntity entity = response.getEntity();
        byte[] responseBody;
        if (entity == null) {
          responseBody = new byte[0];
        } else {
          responseBody = EntityUtils.toByteArray(entity);
          entity.consumeContent();

          final Header encoding = entity.getContentEncoding();
          if (encoding != null) {
            for (HeaderElement element : encoding.getElements()) {
              if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(responseBody));
                responseBody = IoUtils.readAndClose(gis, true);
                break;
              }
            }
          }
        }

        AsyncHttpResponse ahr;
        String statusLine = response.getStatusLine().toString();
        String bodyStr = new String(responseBody);
        int bodySubStrLen = bodyStr.length() > 300 ? 300 : bodyStr.length();

        switch (response.getStatusLine().getStatusCode()) {
          case HttpStatus.SC_OK:
            // Normal success
          case HttpStatus.SC_NOT_MODIFIED:
            // From mobile_config_and_baseurl called with an Etag
          case HttpStatus.SC_MOVED_PERMANENTLY:
          case HttpStatus.SC_SEE_OTHER:
          case HttpStatus.SC_TEMPORARY_REDIRECT:
          case HttpStatus.SC_MOVED_TEMPORARILY:
            // for UPS-1390 - don't error on 302s from token URL
          case HttpStatus.SC_CREATED:
            // Response from the Engage trail creation and maybe URL shortening calls
            LogUtils.logd(statusLine + ": " + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, null, headers, responseBody);
            break;
          default:
            LogUtils.loge(statusLine + "\n" + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, new Exception(statusLine), headers, responseBody);
        }

        mConn.setResponse(ahr);
        invokeCallback(callBack);
      } catch (IOException e) {
        LogUtils.loge(this.toString());
        LogUtils.loge("IOException while executing HTTP request.", e);
        mConn.setResponse(new AsyncHttpResponse(mConn, e, null, null));
        invokeCallback(callBack);
      } catch (AbortedRequestException e) {
        LogUtils.loge("Aborted request: " + mConn.getRequestUrl());
        mConn.setResponse(new AsyncHttpResponse(mConn, null, null, null));
        invokeCallback(callBack);
      }
    }