Пример #1
0
 public static byte[] getBinary(String s, Header aheader[], NameValuePair anamevaluepair[]) {
   byte abyte0[] = null;
   HttpResult httpresult = HttpClientHelper.get(s, aheader, anamevaluepair, null, 0);
   if (httpresult != null && httpresult.getStatusCode() == HttpResult.HTTP_STATUS_CODE_OK)
     abyte0 = httpresult.getResponse();
   return abyte0;
 }
  /**
   * Call to execute post request
   *
   * @param method - url
   * @param envelope - string envelope for post request
   * @param headers - headrs for request
   * @return - HttpResult instance for response
   */
  public static HttpResult post(String method, String envelope, Map<String, String> headers) {
    final DefaultHttpClient httpClient = new DefaultHttpClient();
    /** set params t request */
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, TIMEOUT);
    HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
    /** set url */
    HttpPost httpPost = new HttpPost(method);
    /** set headers */
    if (headers != null) {
      for (Map.Entry<String, String> header : headers.entrySet())
        httpPost.setHeader(header.getKey(), header.getValue());
    }
    /** execute request and generate HttpResult instance */
    final HttpResult resultHttpResult = new HttpResult();
    try {
      /** set envelope for post request */
      envelope = new String(envelope.getBytes("UTF-8"), "UTF-8");
      HttpEntity entity = new StringEntity(envelope, HTTP.UTF_8);
      httpPost.setEntity(entity);
      /** execute request */
      String resultEnvelope =
          httpClient.execute(httpPost, new DefaultResponseHandler(resultHttpResult));
      resultHttpResult.setEnvelope(resultEnvelope);
    } catch (ClientProtocolException ignored) {
    } catch (IOException ignored) {
    }

    return resultHttpResult;
  }
Пример #3
0
 @Test
 public void makePutRequestSendsPut() throws IOException, TmcCoreException, URISyntaxException {
   settings.setUsername("test");
   settings.setPassword("1234");
   Map<String, String> body = new HashMap<>();
   body.put("mark_as_read", "1");
   HttpResult makePutRequest =
       urlCommunicator.makePutRequest(URI.create(serverAddress + "/putty"), Optional.of(body));
   assertEquals(200, makePutRequest.getStatusCode());
 }
 @Override
 public String handleResponse(HttpResponse response)
     throws ClientProtocolException, IOException {
   HttpEntity entity = response.getEntity();
   httpResult.setMessage(response.getStatusLine().getReasonPhrase());
   httpResult.setCode(response.getStatusLine().getStatusCode());
   StringBuilder out = new StringBuilder();
   byte[] data = EntityUtils.toByteArray(entity);
   out.append(new String(data, 0, data.length));
   return out.toString();
 }
Пример #5
0
  @Test
  public void httpPostAddsFileToRequest() throws IOException, TmcCoreException {
    settings.setUsername("test");
    settings.setPassword("1234");
    File testFile = new File("testResources/test.zip");
    HttpResult result =
        urlCommunicator.makePostWithFile(
            new FileBody(testFile),
            URI.create(serverAddress + "/kivaurl"),
            new HashMap<String, String>());

    assertEquals("All tests passed", result.getData());
  }
Пример #6
0
  /**
   * 执行http get请求
   *
   * @param url
   * @return HttpResult
   */
  public HttpResult invoke(String url) {
    GetMethod get = null;
    HttpResult result = null;
    try {
      get = new GetMethod(url);
      getClient().executeMethod(get);
      int statusCode = get.getStatusCode();
      String statusText = get.getStatusText();
      result = new HttpResult(statusCode, statusText);

      if (statusCode == 200) {
        StringBuffer buf = new StringBuffer();
        BufferedReader br =
            new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
        String line;
        while ((line = br.readLine()) != null) {
          buf.append(line).append('\n');
        }
        result.setBody(buf.toString());
      }

    } catch (SocketTimeoutException e) {
      result = new HttpResult(408, "REQUEST TIMEOUT. " + e.getMessage());
    } catch (java.net.ConnectException e) {
      result = new HttpResult(321, "Connection refused. " + e.getMessage());
    } catch (Exception e) {
      LOG.error(e);
      result = new HttpResult(123, e.getMessage());
    } finally {
      if (get != null) {
        try {
          // byte[] bytes = get.getResponseBody();
          get.releaseConnection();
        } catch (Exception e) {
          LOG.error(e);
        }
      }
    }

    return result;
  }
Пример #7
0
  // //////////////////////////////////////
  // ///get bbs list
  // return value is status code
  // "0" not handled error, please communication tech support
  // "1" not login
  // "2" Network communication fail
  // "3" Network communication success
  // "4" Network communication exception
  @Override
  protected ArrayList<ArrayList<String>> doInBackground(String... inputs) {
    ArrayList<ArrayList<String>> outputs = new ArrayList<ArrayList<String>>();

    ArrayList<String> resultList = new ArrayList<String>();
    outputs.add(resultList);

    String hotPostURL = inputs[0];
    String cookie = inputs[1];
    HashMap<String, String> header = new HashMap<String, String>();
    header.put("Cookie", cookie);

    resultList.add("0");
    resultList.add("tmp");

    try {
      HttpResult response = SendHttpRequest.sendGet(hotPostURL, header, null, "utf-8");
      if (response.getStatusCode() == 200) {
        resultList.set(0, "3");
        String pageHtml = EntityUtils.toString(response.getHttpEntity());
        // parse html page content
        parseWebPage(pageHtml, outputs, resultList);
      } else {
        resultList.set(0, "2");
        resultList.set(1, String.valueOf(response.getStatusCode()));
      }
    } catch (ClientProtocolException e) {
      resultList.set(0, "4");
      e.printStackTrace();
    } catch (IOException e) {
      resultList.set(0, "4");
      e.printStackTrace();
    } catch (Exception e) {
      resultList.set(0, "4");
      e.printStackTrace();
    }
    return outputs;
  }
Пример #8
0
 public static String getContent(String s, Header aheader[], NameValuePair anamevaluepair[]) {
   String s1 = null;
   HttpResult httpresult = HttpClientHelper.get(s, aheader, anamevaluepair);
   if (httpresult != null && httpresult.getStatusCode() == 200) s1 = httpresult.getHtml();
   return s1;
 }
Пример #9
0
 @Test
 public void notFoundWithoutValidParams() throws IOException, TmcCoreException {
   HttpResult result =
       urlCommunicator.makeGetRequest(URI.create(serverAddress), "ihanvaaraheaderi:1234");
   assertEquals(403, result.getStatusCode());
 }
Пример #10
0
 @Test
 public void badRequestWithoutValidUrl() throws IOException, TmcCoreException {
   HttpResult result =
       urlCommunicator.makeGetRequest(URI.create(serverAddress + "/vaaraurl"), "test:1234");
   assertEquals(400, result.getStatusCode());
 }
Пример #11
0
 @Test
 public void okWithValidParams() throws IOException, TmcCoreException {
   HttpResult result = urlCommunicator.makeGetRequest(URI.create(serverAddress), "test:1234");
   assertEquals(200, result.getStatusCode());
 }