Example #1
0
  public static void main(String[] args) throws Exception {

    String url = "https://localhost/service/notifyFromMO9.e";
    NotifyToMo9 notify = new NotifyToMo9();

    Map<String, String> formEntity = new TreeMap<String, String>();
    Map<String, String> headerEntity = new TreeMap<String, String>();
    headerEntity.put(
        "User-Agent",
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3");

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-dao.xml");
    PayPartnerManager partnerManager = (PayPartnerManager) context.getBean("payPartnerManager");

    TradeBean trade = new TradeBean();
    trade.setInvoice("881d36e647c64b63a99ab6b654451b51");
    trade.setChannelId(Constants.CHANNEL_ID_MO9 + "");

    formEntity.put("invoice", trade.getInvoice());
    formEntity.put("channelId", trade.getChannelId());

    HttpPost post = new HttpPost(url);
    HttpClient httpClient = HttpUtil.getThreadSafeHttpClient();
    HttpResponse response = HttpUtil.doPost(httpClient, post, formEntity, "UTF-8");

    String content = null;
    if (response.getEntity() != null) {
      String charset =
          EntityUtils.getContentCharSet(response.getEntity()) == null
              ? "UTF-8"
              : EntityUtils.getContentCharSet(response.getEntity());
      content = new String(EntityUtils.toByteArray(response.getEntity()), charset);
    }
    System.out.println(content);
  }
Example #2
0
 public String handleResponse(HttpResponse response)
     throws ClientProtocolException, IOException {
   HttpEntity entity = response.getEntity();
   if (entity != null) {
     String charset =
         EntityUtils.getContentCharSet(entity) == null
             ? CHARSET_UTF8
             : EntityUtils.getContentCharSet(entity);
     return new String(EntityUtils.toByteArray(entity), charset);
   } else {
     return null;
   }
 }
  /**
   * 将Entity的内容载入Page对象
   *
   * @date 2013-1-7 上午11:22:06
   * @param entity
   * @return
   * @throws Exception
   */
  private Page load(HttpEntity entity) throws Exception {
    Page page = new Page();

    // 设置返回内容的ContentType
    String contentType = null;
    Header type = entity.getContentType();
    if (type != null) contentType = type.getValue();
    page.setContentType(contentType);

    // 设置返回内容的字符编码
    String contentEncoding = null;
    Header encoding = entity.getContentEncoding();
    if (encoding != null) contentEncoding = encoding.getValue();
    page.setEncoding(contentEncoding);

    // 设置返回内容的字符集
    String contentCharset = EntityUtils.getContentCharSet(entity);
    page.setCharset(contentCharset);
    // 根据配置文件设置的字符集参数进行内容二进制话
    String charset = config.getCharset();
    String content = this.read(entity.getContent(), charset);
    page.setContent(content);
    //		if (charset == null || charset.trim().length() == 0)
    //			page.setContentData(content.getBytes());
    //		else
    //			page.setContentData(content.getBytes(charset));

    return page;
  }
Example #4
0
 private Response handleHttpRequest(HttpRequestBase httpRequestBase) throws IOException {
   HttpParams httpParams = new BasicHttpParams();
   HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
   HttpConnectionParams.setSoTimeout(httpParams, timeout);
   HttpClient httpClient = new DefaultHttpClient(httpParams);
   byte[] content = null;
   String charset;
   HttpResponse httpResponse;
   Header cookie;
   try {
     httpResponse = httpClient.execute(httpRequestBase);
     cookie = httpResponse.getLastHeader("Set-Cookie");
     //			Log.d("remote", "cookie: " + cookie);
     HttpEntity httpEntity = httpResponse.getEntity();
     if (httpEntity != null) {
       ByteArrayOutputStream bos = new ByteArrayOutputStream();
       InputStream is = httpEntity.getContent();
       IoUtil.ioAndClose(is, bos);
       content = bos.toByteArray();
     }
     charset = EntityUtils.getContentCharSet(httpEntity);
   } finally {
     try {
       httpClient.getConnectionManager().shutdown();
     } catch (Exception ignore) {
       // ignore it
     }
   }
   return responseParser.parse(content, charset, parseSessionId(cookie));
 }
    protected String[] doInBackground(String... params) {
      // SAAJAN
      HttpURLConnection urlConnection = null;
      try {
        final String FORECAST_BASE_URL = Utility.BASE_URL + "/api/registration/";
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

        String FirstName = params[0].toString();
        String LastName = params[1].toString();
        String UserName = params[2].toString();
        String UserEmail = params[3].toString();
        String UserPassword = params[4].toString();

        nameValuePairs.add(new BasicNameValuePair("username", UserName));
        nameValuePairs.add(new BasicNameValuePair("email", UserEmail));
        nameValuePairs.add(new BasicNameValuePair("password", UserPassword));
        nameValuePairs.add(new BasicNameValuePair("first_name", FirstName));
        nameValuePairs.add(new BasicNameValuePair("last_name", LastName));

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(FORECAST_BASE_URL);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);

        String encoding = EntityUtils.getContentCharSet(response.getEntity());
        encoding = encoding == null ? "UTF-8" : encoding;
        InputStream stream = AndroidHttpClient.getUngzippedContent(response.getEntity());
        InputStreamEntity unzEntity = new InputStreamEntity(stream, -1);
        String RegistrationAttemptResponse = EntityUtils.toString(unzEntity, encoding);

        System.out.println("Registration attempt response: " + RegistrationAttemptResponse);

        if (RegistrationAttemptResponse.length() > 90) {
          parseResponseTakeAction(RegistrationAttemptResponse);
        }

        return null;
      } catch (Exception e) {
        Log.e(LOG_TAG, "Error ", e);
        // If the code didn't successfully get the weather data, there's no point in attemping
        // to parse it.
        return null;
      } finally {
        if (urlConnection != null) {
          urlConnection.disconnect();
        }
        return null;
      }
      //
    }
Example #6
0
  /**
   * Get file
   *
   * @param url
   * @return
   * @throws Exception
   */
  public static String basicHttpAccess(String url) throws Exception {
    RequestConfig config =
        RequestConfig.custom()
            .setConnectionRequestTimeout(30 * 1000) // 30 sec
            .setConnectTimeout(30 * 1000) // 30sec
            .build();
    HttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    HttpPost httppost = new HttpPost(url);
    httppost.setConfig(config);
    try {
      // Execute and get the response.
      HttpResponse response = httpclient.execute(httppost);

      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // check HttpStatus
        HttpEntity entity = response.getEntity();
        if (entity != null) {

          @SuppressWarnings("deprecation")
          String origCharset = EntityUtils.getContentCharSet(entity); // get charset
          // get content from entity
          InputStream instream = entity.getContent();
          String content = MyCharset.convertCharset(instream, origCharset, MyContext.Charset);

          // clear resouce
          instream.close();

          // return
          return content;
        }
      } else {
        throw new Exception(
            "url:"
                + url
                + " http request fetch failed. http response status code is "
                + response.getStatusLine().getStatusCode());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw ex;
    } finally {
      // clear resouce
      httppost = null;
      httpclient = null;
    }
    return null;
  }
Example #7
0
 public static String getStringFromResponse(HttpResponse response)
     throws IOException, UnsupportedEncodingException {
   String charset = null, total = "";
   HttpEntity entity = response.getEntity();
   if (entity != null) {
     if (charset == null) charset = EntityUtils.getContentCharSet(entity);
     BufferedReader br = null;
     if (charset == null || charset == "")
       br = new BufferedReader(new InputStreamReader(entity.getContent()));
     else br = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
     String line = null;
     while ((line = br.readLine()) != null) {
       total += line + "\n";
       // logger.info(line);
     }
   }
   return total;
 }
Example #8
0
    @SuppressWarnings("deprecation")
    @Override
    protected JSONObject doInBackground(Void... params) {

      try {
        array = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UserID", Integer.parseInt(Storage.getinstance().getString("UserID")));
        jsonObject.put("UserName", Storage.getinstance().getString("UserName"));
        jsonObject.put("Password", Storage.getinstance().getString("Password"));
        jsonObject.put(
            "AuthenticationToken", Storage.getinstance().getString("AuthenticationToken"));
        jsonObject.put("EntityType", "");

        array.put(jsonObject);
        Log.e("Array", array.toString());

        // HttpPost httpPost = new HttpPost(new
        // URI("http://192.168.0.112/eua/portalresource.aspx?rt=2&js=Logoff"));
        // HttpPost httpPost = new HttpPost(new
        // URI("http://exproject.excelloncloud.com:90/portalresource.aspx?rt=2&js=Logoff"));
        HttpPost httpPost = new HttpPost(new URI(AppConstant.PayBillURL_Local));
        Log.e("Array Post", array.toString());
        httpPost.setEntity(new UrlEncodedFormEntity(getNameValuePairs("Logoff", array.toString())));
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity entity = httpResponse.getEntity();
        String strCharSet = EntityUtils.getContentCharSet(entity);
        String strResponse = EntityUtils.toString(entity, strCharSet);
        sumbmitResponse = new JSONObject(convertStandardJSONString(strResponse.toString()));
        sumbmitResponse = new JSONObject(strResponse.toString());

      } catch (Exception e) {
        Log.e("weerre", "" + e);
      }
      return sumbmitResponse;
    }
Example #9
0
  public NamedList<Object> request(final SolrRequest request, final ResponseParser processor)
      throws SolrServerException, IOException {
    HttpRequestBase method = null;
    InputStream is = null;
    SolrParams params = request.getParams();
    Collection<ContentStream> streams = requestWriter.getContentStreams(request);
    String path = requestWriter.getPath(request);
    if (path == null || !path.startsWith("/")) {
      path = DEFAULT_PATH;
    }

    ResponseParser parser = request.getResponseParser();
    if (parser == null) {
      parser = this.parser;
    }

    // The parser 'wt=' and 'version=' params are used instead of the original
    // params
    ModifiableSolrParams wparams = new ModifiableSolrParams(params);
    if (parser != null) {
      wparams.set(CommonParams.WT, parser.getWriterType());
      wparams.set(CommonParams.VERSION, parser.getVersion());
    }
    if (invariantParams != null) {
      wparams.add(invariantParams);
    }

    int tries = maxRetries + 1;
    try {
      while (tries-- > 0) {
        // Note: since we aren't do intermittent time keeping
        // ourselves, the potential non-timeout latency could be as
        // much as tries-times (plus scheduling effects) the given
        // timeAllowed.
        try {
          if (SolrRequest.METHOD.GET == request.getMethod()) {
            if (streams != null) {
              throw new SolrException(
                  SolrException.ErrorCode.BAD_REQUEST, "GET can't send streams!");
            }
            method = new HttpGet(baseUrl + path + ClientUtils.toQueryString(wparams, false));
          } else if (SolrRequest.METHOD.POST == request.getMethod()) {

            String url = baseUrl + path;
            boolean hasNullStreamName = false;
            if (streams != null) {
              for (ContentStream cs : streams) {
                if (cs.getName() == null) {
                  hasNullStreamName = true;
                  break;
                }
              }
            }
            boolean isMultipart =
                (this.useMultiPartPost || (streams != null && streams.size() > 1))
                    && !hasNullStreamName;

            // only send this list of params as query string params
            ModifiableSolrParams queryParams = new ModifiableSolrParams();
            for (String param : this.queryParams) {
              String[] value = wparams.getParams(param);
              if (value != null) {
                for (String v : value) {
                  queryParams.add(param, v);
                }
                wparams.remove(param);
              }
            }

            LinkedList<NameValuePair> postParams = new LinkedList<NameValuePair>();
            if (streams == null || isMultipart) {
              HttpPost post = new HttpPost(url + ClientUtils.toQueryString(queryParams, false));
              post.setHeader("Content-Charset", "UTF-8");
              if (!isMultipart) {
                post.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
              }

              List<FormBodyPart> parts = new LinkedList<FormBodyPart>();
              Iterator<String> iter = wparams.getParameterNamesIterator();
              while (iter.hasNext()) {
                String p = iter.next();
                String[] vals = wparams.getParams(p);
                if (vals != null) {
                  for (String v : vals) {
                    if (isMultipart) {
                      parts.add(new FormBodyPart(p, new StringBody(v, Charset.forName("UTF-8"))));
                    } else {
                      postParams.add(new BasicNameValuePair(p, v));
                    }
                  }
                }
              }

              if (isMultipart && streams != null) {
                for (ContentStream content : streams) {
                  String contentType = content.getContentType();
                  if (contentType == null) {
                    contentType = BinaryResponseParser.BINARY_CONTENT_TYPE; // default
                  }
                  String name = content.getName();
                  if (name == null) {
                    name = "";
                  }
                  parts.add(
                      new FormBodyPart(
                          name,
                          new InputStreamBody(
                              content.getStream(), contentType, content.getName())));
                }
              }

              if (parts.size() > 0) {
                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.STRICT);
                for (FormBodyPart p : parts) {
                  entity.addPart(p);
                }
                post.setEntity(entity);
              } else {
                // not using multipart
                post.setEntity(new UrlEncodedFormEntity(postParams, "UTF-8"));
              }

              method = post;
            }
            // It is has one stream, it is the post body, put the params in the URL
            else {
              String pstr = ClientUtils.toQueryString(wparams, false);
              HttpPost post = new HttpPost(url + pstr);

              // Single stream as body
              // Using a loop just to get the first one
              final ContentStream[] contentStream = new ContentStream[1];
              for (ContentStream content : streams) {
                contentStream[0] = content;
                break;
              }
              if (contentStream[0] instanceof RequestWriter.LazyContentStream) {
                post.setEntity(
                    new InputStreamEntity(contentStream[0].getStream(), -1) {
                      @Override
                      public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                      }

                      @Override
                      public boolean isRepeatable() {
                        return false;
                      }
                    });
              } else {
                post.setEntity(
                    new InputStreamEntity(contentStream[0].getStream(), -1) {
                      @Override
                      public Header getContentType() {
                        return new BasicHeader("Content-Type", contentStream[0].getContentType());
                      }

                      @Override
                      public boolean isRepeatable() {
                        return false;
                      }
                    });
              }
              method = post;
            }
          } else {
            throw new SolrServerException("Unsupported method: " + request.getMethod());
          }
        } catch (NoHttpResponseException r) {
          method = null;
          if (is != null) {
            is.close();
          }
          // If out of tries then just rethrow (as normal error).
          if (tries < 1) {
            throw r;
          }
        }
      }
    } catch (IOException ex) {
      throw new SolrServerException("error reading streams", ex);
    }

    // XXX client already has this set, is this needed?
    method.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, followRedirects);
    method.addHeader("User-Agent", AGENT);

    InputStream respBody = null;
    boolean shouldClose = true;
    boolean success = false;
    try {
      // Execute the method.
      final HttpResponse response = httpClient.execute(method);
      int httpStatus = response.getStatusLine().getStatusCode();

      // Read the contents
      respBody = response.getEntity().getContent();
      Header ctHeader = response.getLastHeader("content-type");
      String contentType;
      if (ctHeader != null) {
        contentType = ctHeader.getValue();
      } else {
        contentType = "";
      }

      // handle some http level checks before trying to parse the response
      switch (httpStatus) {
        case HttpStatus.SC_OK:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_CONFLICT: // 409
          break;
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
          if (!followRedirects) {
            throw new SolrServerException(
                "Server at " + getBaseURL() + " sent back a redirect (" + httpStatus + ").");
          }
          break;
        default:
          if (processor == null) {
            throw new RemoteSolrException(
                httpStatus,
                "Server at "
                    + getBaseURL()
                    + " returned non ok status:"
                    + httpStatus
                    + ", message:"
                    + response.getStatusLine().getReasonPhrase(),
                null);
          }
      }
      if (processor == null) {

        // no processor specified, return raw stream
        NamedList<Object> rsp = new NamedList<Object>();
        rsp.add("stream", respBody);
        // Only case where stream should not be closed
        shouldClose = false;
        success = true;
        return rsp;
      }

      String procCt = processor.getContentType();
      if (procCt != null) {
        if (!contentType.equals(procCt)) {
          // unexpected content type
          String msg = "Expected content type " + procCt + " but got " + contentType + ".";
          Header encodingHeader = response.getEntity().getContentEncoding();
          String encoding;
          if (encodingHeader != null) {
            encoding = encodingHeader.getValue();
          } else {
            encoding = "UTF-8"; // try UTF-8
          }
          try {
            msg = msg + " " + IOUtils.toString(respBody, encoding);
          } catch (IOException e) {
            throw new RemoteSolrException(
                httpStatus, "Could not parse response with encoding " + encoding, e);
          }
          RemoteSolrException e = new RemoteSolrException(httpStatus, msg, null);
          throw e;
        }
      }

      //      if(true) {
      //        ByteArrayOutputStream copy = new ByteArrayOutputStream();
      //        IOUtils.copy(respBody, copy);
      //        String val = new String(copy.toByteArray());
      //        System.out.println(">RESPONSE>"+val+"<"+val.length());
      //        respBody = new ByteArrayInputStream(copy.toByteArray());
      //      }

      NamedList<Object> rsp = null;
      String charset = EntityUtils.getContentCharSet(response.getEntity());
      try {
        rsp = processor.processResponse(respBody, charset);
      } catch (Exception e) {
        throw new RemoteSolrException(httpStatus, e.getMessage(), e);
      }
      if (httpStatus != HttpStatus.SC_OK) {
        String reason = null;
        try {
          NamedList err = (NamedList) rsp.get("error");
          if (err != null) {
            reason = (String) err.get("msg");
            // TODO? get the trace?
          }
        } catch (Exception ex) {
        }
        if (reason == null) {
          StringBuilder msg = new StringBuilder();
          msg.append(response.getStatusLine().getReasonPhrase());
          msg.append("\n\n");
          msg.append("request: " + method.getURI());
          reason = java.net.URLDecoder.decode(msg.toString(), UTF_8);
        }
        throw new RemoteSolrException(httpStatus, reason, null);
      }
      success = true;
      return rsp;
    } catch (ConnectException e) {
      throw new SolrServerException("Server refused connection at: " + getBaseURL(), e);
    } catch (SocketTimeoutException e) {
      throw new SolrServerException(
          "Timeout occured while waiting response from server at: " + getBaseURL(), e);
    } catch (IOException e) {
      throw new SolrServerException(
          "IOException occured when talking to server at: " + getBaseURL(), e);
    } finally {
      if (respBody != null && shouldClose) {
        try {
          respBody.close();
        } catch (Throwable t) {
        } // ignore
        if (!success) {
          method.abort();
        }
      }
    }
  }
Example #10
0
  public void execute() throws Exception {
    HttpRequestBaseHC4 request;
    if (isPost()) {
      request = new HttpPostHC4(uri);
      if (onHttpRequestListener != null) onHttpRequestListener.onRequest(this);
      if (!requestParams.isEmpty()) {
        try {
          ((HttpPostHC4) request).setEntity(new UrlEncodedFormEntity(requestParams, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }
    } else {
      if (onHttpRequestListener != null) onHttpRequestListener.onRequest(this);
      if (!requestParams.isEmpty()) {
        try {
          URI oldUri = new URI(uri);
          String query = "";
          if (oldUri.getQuery() != null && !oldUri.getQuery().equals(""))
            query += oldUri.getQuery() + "&";
          query += URLEncodedUtils.format(requestParams, "UTF-8");
          URI newUri =
              URIUtils.createURI(
                  oldUri.getScheme(),
                  oldUri.getHost(),
                  oldUri.getPort(),
                  oldUri.getPath(),
                  query,
                  null);
          uri = newUri.toString();
        } catch (URISyntaxException e) {
          e.printStackTrace();
        }
      }
      request = new HttpGetHC4(uri);
    }

    RequestConfig rc =
        RequestConfig.custom()
            .setSocketTimeout(socketTimeout)
            .setConnectTimeout(connectTimeout)
            .setConnectionRequestTimeout(connectionRequestTimeout)
            .build();
    request.setConfig(rc);

    try {
      CloseableHttpResponse response = CpHttpClient.getHttpClient().execute(request, context);
      try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.getEntity().writeTo(baos);
        // 将数据转换为字符串保存
        String respCharset = EntityUtils.getContentCharSet(response.getEntity());
        if (respCharset != null) result = new String(baos.toByteArray(), respCharset);
        else result = new String(baos.toByteArray(), "UTF-8");
        if (onHttpRequestListener != null) {
          int statusCode = response.getStatusLine().getStatusCode();
          if (statusCode == HttpStatus.SC_OK) onHttpRequestListener.onSucceed(statusCode, this);
          else onHttpRequestListener.onFailed(statusCode, this);
        }
      } finally {
        response.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
      CpHttpClient.shutDown();
    }
  }
  private Map<Attribute, String> invokeWebService(
      URI serviceTargetURI, List<NameValuePair> formparams)
      throws VirtualCollectionRegistryException {
    // force xml encoding
    formparams.add(new BasicNameValuePair("encoding", "xml"));

    DefaultHttpClient client = null;
    try {
      client = new DefaultHttpClient();
      int port = serviceTargetURI.getPort() != -1 ? serviceTargetURI.getPort() : AuthScope.ANY_PORT;
      client
          .getCredentialsProvider()
          .setCredentials(
              new AuthScope(serviceTargetURI.getHost(), port),
              new UsernamePasswordCredentials(username, password));
      // disable expect continue, GWDG does not like very well
      client.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
      // set a proper user agent
      client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, USER_AGENT);
      HttpPost request = new HttpPost(serviceTargetURI);
      request.addHeader("Accept", "text/xml, application/xml");
      request.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
      HttpContext ctx = new BasicHttpContext();

      logger.debug("invoking GWDG service at {}", serviceTargetURI);
      HttpResponse response = client.execute(request, ctx);
      StatusLine status = response.getStatusLine();
      HttpEntity entity = response.getEntity();
      Map<Attribute, String> props = Collections.emptyMap();

      logger.debug("GWDG Service status: {}", status.toString());
      if ((status.getStatusCode() >= 200) && (status.getStatusCode() <= 299) && (entity != null)) {
        String encoding = EntityUtils.getContentCharSet(entity);
        if (encoding == null) {
          encoding = "UTF-8";
        }

        XMLStreamReader reader = factory.createXMLStreamReader(entity.getContent(), encoding);
        props = new HashMap<Attribute, String>();
        while (reader.hasNext()) {
          reader.next();

          int type = reader.getEventType();
          if (type != XMLStreamConstants.START_ELEMENT) {
            continue;
          }
          Attribute attribute = Attribute.fromString(reader.getLocalName());
          if (attribute != null) {
            if (!reader.hasNext()) {
              throw new VirtualCollectionRegistryException("unexpected end of data stream");
            }
            reader.next();
            if (reader.getEventType() != XMLStreamConstants.CHARACTERS) {
              throw new VirtualCollectionRegistryException(
                  "unexpected element type: " + reader.getEventType());
            }
            String value = reader.getText();
            if (value == null) {
              throw new VirtualCollectionRegistryException(
                  "element \"" + attribute + "\" was empty");
            }
            value = value.trim();
            if (!value.isEmpty()) {
              props.put(attribute, value);
            }
          }
        }

      } else {
        logger.debug("GWDG Handle service failed: {}", status);
        request.abort();
        throw new VirtualCollectionRegistryException("error invoking GWDG handle service");
      }
      return props;
    } catch (VirtualCollectionRegistryException e) {
      throw e;
    } catch (Exception e) {
      logger.debug("GWDG Handle service failed", e);
      throw new VirtualCollectionRegistryException("error invoking GWDG handle service", e);
    } finally {
      if (client != null) {
        client.getConnectionManager().shutdown();
      }
    }
  }
Example #12
0
 private void setResponseData(HttpEntity entity) throws IOException, ParseException {
   if (entity != null) {
     responseData = TiBlob.blobFromData(proxy.getTiContext(), EntityUtils.toByteArray(entity));
     charset = EntityUtils.getContentCharSet(entity);
   }
 }
Example #13
0
    public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
      connected = true;
      String clientResponse = null;

      if (client != null) {
        TiHTTPClient c = client.get();
        if (c != null) {
          c.response = response;
          c.setReadyState(READY_STATE_HEADERS_RECEIVED);
          c.setStatus(response.getStatusLine().getStatusCode());
          c.setStatusText(response.getStatusLine().getReasonPhrase());
          c.setReadyState(READY_STATE_LOADING);
        }

        if (DBG) {
          try {
            Log.w(LCAT, "Entity Type: " + response.getEntity().getClass());
            Log.w(LCAT, "Entity Content Type: " + response.getEntity().getContentType().getValue());
            Log.w(LCAT, "Entity isChunked: " + response.getEntity().isChunked());
            Log.w(LCAT, "Entity isStreaming: " + response.getEntity().isStreaming());
          } catch (Throwable t) {
            // Ignore
          }
        }

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
          setResponseText(response.getEntity());
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        entity = response.getEntity();
        if (entity.getContentType() != null) {
          contentType = entity.getContentType().getValue();
        }
        KrollCallback onDataStreamCallback = c.getCallback(ON_DATA_STREAM);
        if (onDataStreamCallback != null) {
          is = entity.getContent();
          charset = EntityUtils.getContentCharSet(entity);

          responseData = null;

          if (is != null) {
            final KrollCallback cb = onDataStreamCallback;
            long contentLength = entity.getContentLength();
            if (DBG) {
              Log.d(LCAT, "Content length: " + contentLength);
            }
            int count = 0;
            int totalSize = 0;
            byte[] buf = new byte[4096];
            if (DBG) {
              Log.d(LCAT, "Available: " + is.available());
            }
            if (aborted) {
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              while ((count = is.read(buf)) != -1) {
                totalSize += count;
                TiDict o = new TiDict();
                o.put("totalCount", contentLength);
                o.put("totalSize", totalSize);
                o.put("size", count);

                byte[] newbuf = new byte[count];
                System.arraycopy(buf, 0, newbuf, 0, count);
                if (responseData == null) {
                  responseData = TiBlob.blobFromData(proxy.getTiContext(), newbuf, contentType);
                } else {
                  responseData.append(TiBlob.blobFromData(proxy.getTiContext(), newbuf));
                }

                TiBlob blob = TiBlob.blobFromData(proxy.getTiContext(), newbuf);
                o.put("blob", blob);
                o.put("progress", ((double) totalSize) / ((double) contentLength));

                cb.callWithProperties(o);
              }
              if (entity != null) {
                try {
                  entity.consumeContent();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }
          }
        } else {
          setResponseData(entity);
        }
      }
      return clientResponse;
    }
Example #14
0
 public String charset() {
   return EntityUtils.getContentCharSet(entity());
 }
Example #15
0
  /**
   * @param url
   * @param check 是否检测有些好的spider
   * @param charset
   * @return
   */
  public static PageData getPage(
      String url,
      boolean check,
      String charset,
      HttpClient client,
      HttpContext context,
      String[] headers) {
    // TODO Auto-generated method stub
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
    HttpConnectionParams.setSoTimeout(httpParams, 30000);
    boolean shutdown = false;
    if (client == null) {
      shutdown = true;
      client = new DefaultHttpClient(httpParams);
    }
    if (context == null) context = new BasicHttpContext();

    // logger.info(context + " " + client);

    HttpGet get = new HttpGet(url);
    if (url.contains("google")) {

      client = WebClientDevWrapper.wrapClient(client);
    }
    if (url.contains("google")) {
      get.setHeader(
          "cookie",
          "PREF=ID=9a471391775d2718:U=017be7dce7b55194:FF=1:LD=zh-CN:NW=1:TM=1382695331:LM=1386039904:GM=1:S=SvQnJWl081TFsWb7; HSID=A066eSNsZTseWRtOK; SSID=AV-7nyoQs45__A4V9; APISID=X0-97mhCRFpjclPE/ADd_Lk9TSH7-Etz-3; SAPISID=1c5b5JpKbcF4iQ0o/Ajw7cQopZF1r72g5l; SID=DQAAAMAAAAAh_aa2SI2mUBcQwXxD7ZaIcbm_lVeaBz7yBR5JvJi_3nX13QwGLknTzpkO4zYy8hKVtuQ6A3Je0k1uOcIQ1l6gGLA-EvD2pe0GvklG8Dohi6RdzlHbDJfN6hlOu6X_TazhAYg0vQZTPJvrHoEPuVNcN_damYs7huAd0VMZcxhypXTVME-iDbo4M-w8ceHXrcbsNpZ8LbqQjts0HCX4ZxCZgidIH6gr-FgeFn2nsSO2aZPagNOS2QOL0ibVmF4wWo0; NID=67=wfsWc4-JhuSOPFcOSHVzr4i2uA-8NABZv_IA8w2a4k-HG1TzUibg5SQuV3W2SltDEj7msm_0xjOhd7K7WN8m0lCDQGVerJxcPi7WJ4dX5lp9wG7I4nNMLG9f_xWd7mJOeFu-B5Oth07fCGYgbTcqST-l8IrhOMUE5xUzWXJuXW_27_eY3wsK9oEfPe_wcyhA2fVTxss-4CA");
      get.setHeader(
          "x-chrome-variations", "CP21yQEIhrbJAQiktskBCKm2yQEIxLbJAQiehsoBCNyHygEIlorKAQ==");
    }
    get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
    get.setHeader(
        "accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    get.setHeader(
        "user-agent",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
    get.setHeader("pragma", "no-cache");
    if (headers != null) {
      for (int i = 0; i < headers.length; i += 2) {
        get.setHeader(headers[i], headers[i + 1]);
      }
    }

    PageData pd = null;
    String total = "";
    try {
      HttpResponse response = client.execute(get, context);
      HttpUriRequest actualRequest =
          (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
      HttpHost host = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

      //			if(check && !checkHost(host.toString())){
      // logger.info(host + "   没有spider,返回!" );
      //				return null;
      //			}
      if (response.getStatusLine().getStatusCode() == 404) {
        logger.info("404! ");
        logger.info(url);
        return null;
      }
      HttpEntity entity = response.getEntity();

      if (entity != null) {
        if (charset == null) charset = EntityUtils.getContentCharSet(entity);

        if (charset == null) {
          if (url.contains("blog.163")) charset = "gbk";
          else if (url.contains("acm.hdu.edu") || url.contains("blog.51cto.com")) charset = "gbk";
          else charset = "utf-8";
        }
        // logger.info("charset:" + charset + "  ;  " + url );
        BufferedReader br = null;
        if (charset == null || charset == "")
          br = new BufferedReader(new InputStreamReader(entity.getContent()));
        else br = new BufferedReader(new InputStreamReader(entity.getContent(), charset));
        String line = null;
        while ((line = br.readLine()) != null) {
          total += line + "\n";
          // logger.info(line);
        }

        //				logger.info("charset: " + charset);
        //				if(charset != null && !charset.toLowerCase().contains("utf")){
        //					total = new String(total.getBytes(charset), "utf-8");
        //				}
        pd =
            new PageData(
                total, host.toString(), host.toString() + actualRequest.getURI().toString());

        br.close();
      }

      if (shutdown) client.getConnectionManager().shutdown();
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return pd;
  }
 private void setResponseData(HttpEntity entity) throws IOException, ParseException {
   if (entity != null) {
     responseData = EntityUtils.toByteArray(entity);
     charset = EntityUtils.getContentCharSet(entity);
   }
 }