Ejemplo n.º 1
0
  /**
   * Sends a POST request with a file and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param file The file to upload
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param fields Any name-value pairs to serialise
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   * @see MultipartEntityBuilder
   */
  public <T> Response<T> post(
      Endpoint endpoint, File file, Class<T> responseClass, NameValuePair... fields)
      throws IOException {
    if (file == null) {
      return post(endpoint, responseClass, fields);
    } // deal with null case

    // Create the request
    HttpPost post = new HttpPost(endpoint.url());
    post.setHeaders(combineHeaders());

    // Add fields as text pairs
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    for (NameValuePair field : fields) {
      multipartEntityBuilder.addTextBody(field.getName(), field.getValue());
    }
    // Add file as binary
    FileBody bin = new FileBody(file);
    multipartEntityBuilder.addPart("file", bin);

    // Set the body
    post.setEntity(multipartEntityBuilder.build());

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(post)) {
      T body = deserialiseResponseMessage(response, responseClass);
      return new Response<>(response.getStatusLine(), body);
    }
  }
Ejemplo n.º 2
0
  /**
   * Sends a POST request with a file and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param file The file to upload
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   * @see MultipartEntityBuilder
   */
  private <T> Response<T> post(Endpoint endpoint, File file, Class<T> responseClass)
      throws IOException {
    if (file == null) {
      return post(endpoint, responseClass);
    } // deal with null case

    // Create the request
    HttpPost post = new HttpPost(endpoint.url());
    post.setHeaders(combineHeaders());

    // Add fields as text pairs
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    // Add file as binary
    FileBody fileBody = new FileBody(file);
    multipartEntityBuilder.addPart("file", fileBody);

    // Set the body
    post.setEntity(multipartEntityBuilder.build());

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(post)) {
      if (String.class.isAssignableFrom(responseClass)) {
        String body = getResponseString(response);
        return new Response(response.getStatusLine(), body);
      } else {
        T body = deserialiseResponseMessage(response, responseClass);
        return new Response<>(response.getStatusLine(), body);
      }
    }
  }
  /**
   * @param request
   * @return
   */
  public static HttpPost convertToHttpPost(BasicHttpEntityEnclosingRequest request) {

    HttpPost post = null;
    post = new HttpPost(request.getRequestLine().getUri());
    post.setHeaders(request.getAllHeaders());
    post.setEntity(request.getEntity());
    post.setParams(request.getParams());
    return post;
  }
  private static void addHeaders(HttpPost r) {
    Header[] v =
        new Header[] {
          new BasicHeader("Content-Type", "application/json"),
          //				new BasicHeader("Authorization", "APP " + mSOAAppName),
          //				new BasicHeader("Accept", "application/xml"),
        };
    //			Log(v);

    r.setHeaders(v);
  }
Ejemplo n.º 5
0
 public void post(
     Context paramContext,
     String paramString1,
     Header[] paramArrayOfHeader,
     aq paramaq,
     String paramString2,
     an paraman) {
   HttpPost localHttpPost = new HttpPost(paramString1);
   if (paramaq != null) localHttpPost.setEntity(a(paramaq));
   if (paramArrayOfHeader != null) localHttpPost.setHeaders(paramArrayOfHeader);
   a(paramContext, getHttpClient(), getHttpContext(), null, localHttpPost, paramString2, paraman);
 }
Ejemplo n.º 6
0
 public void getIPTShows() {
   CloseableHttpClient httpClient = HttpClientBuilder.create().build();
   CloseableHttpResponse response = null;
   String pageURL = "https://www.iptorrents.com";
   try {
     HttpGet httpGet = new HttpGet(pageURL);
     httpGet.addHeader(
         "User-Agent",
         "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36");
     response = httpClient.execute(httpGet);
     response.removeHeaders("Transfer-Encoding");
     HttpPost thePost = new HttpPost(pageURL + "?username=mcpchelper81&password=ru68ce48&php=");
     thePost.setHeaders(response.getAllHeaders());
     response.close();
     response = null;
     response = httpClient.execute(thePost);
     httpGet = new HttpGet("https://www.iptorrents.com/t?5");
     httpGet.setHeaders(response.getHeaders("set-cookie"));
     httpGet.addHeader(
         "accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
     httpGet.addHeader("accept-encoding", "gzip, deflate, sdch");
     httpGet.addHeader("accept-language", "en-US,en;q=0.8");
     httpGet.addHeader("dnt", "1");
     httpGet.addHeader("upgrade-insecure-requests", "1");
     httpGet.addHeader(
         "user-agent",
         "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36");
     response.close();
     response = null;
     response = httpClient.execute(httpGet);
     Header contentType = response.getFirstHeader("Content-Type");
     HttpEntity httpEntity = response.getEntity();
     String[] contentArray = contentType.getValue().split(";");
     String charset = "UTF-8";
     if (contentArray.length > 1 && contentArray[1].contains("=")) {
       charset = contentArray[1].trim().split("=")[1];
     }
     Document pageDoc = Jsoup.parse(httpEntity.getContent(), charset, httpGet.getURI().getPath());
     Elements results = pageDoc.getElementsByClass("torrents");
     response.close();
     Elements rawShowObjects = results.select("tr");
     IPTToTvShowEpisode makeShows = new IPTToTvShowEpisode();
     List<TvShowEpisode> theShows = makeShows.makeTSEBeans(rawShowObjects);
     DBActions.insertIPTTvEpisodes(theShows, "https://www.iptorrents.com/t?5");
   } catch (MalformedURLException MURLe) {
     MURLe.printStackTrace();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 7
0
  /**
   * Sends a POST request and returns the response.
   *
   * <p>Specifically for the use case where we have no requestMessage
   *
   * @param endpoint The endpoint to send the request to.
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param headers Any additional headers to send with this request. You can use {@link
   *     org.apache.http.HttpHeaders} constants for header names.
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   */
  public <T> Response<T> post(Endpoint endpoint, Class<T> responseClass, NameValuePair... headers)
      throws IOException {

    // Create the request
    HttpPost post = new HttpPost(endpoint.url());
    post.setHeaders(combineHeaders(headers));

    // Add the request message if there is one
    post.setEntity(serialiseRequestMessage(null));

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(post)) {
      T body = deserialiseResponseMessage(response, responseClass);
      return new Response<>(response.getStatusLine(), body);
    }
  }
Ejemplo n.º 8
0
  protected static HttpResponse doPost(String resource, String body, Header[] headers)
      throws Exception {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(AppFabricTestBase.getEndPoint(resource));

    if (body != null) {
      post.setEntity(new StringEntity(body));
    }

    if (headers != null) {
      post.setHeaders(ObjectArrays.concat(AUTH_HEADER, headers));
    } else {
      post.setHeader(AUTH_HEADER);
    }
    return client.execute(post);
  }
Ejemplo n.º 9
0
  public static void testPost() throws IOException {
    // 客户端
    HttpClient client = new DefaultHttpClient();

    ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();

    //        HttpPost post = new HttpPost("http://127.0.0.1/login");
    //        HttpPost post = new HttpPost("http://jasic.vicp.net/platform/token/gh_b817172873c4");
    HttpPost post = new HttpPost("http://localhost:8080/terminal/cmd");

    Header Agent =
        new BasicHeader(
            "User-Agent",
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36");
    //        Header referer = new BasicHeader("Referer", "http://hg0088.com/app/member/");
    Header connection = new BasicHeader("Connection", "keep-alive");

    Header accept;
    //        accept = new BasicHeader("Accept",
    // "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    accept = new BasicHeader("Accept", "application/json");

    Header contentType;
    //        contentType= new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
    contentType = new BasicHeader("Content-Type", "application/json");

    Header cacheControl = new BasicHeader("Cache-Control", "max-age=0");
    //        Header origin = new BasicHeader("Origin", "http://hg0088.com");
    Header acceptEncoding = new BasicHeader("Accept-Encoding", "gzip,deflate,sdch");
    Header acceptLanguage = new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8");

    post.setHeaders(
        new Header[] {
          Agent, connection, accept, cacheControl, contentType, acceptEncoding, acceptLanguage
        });
    String msg = null;
    // ================================================================设置内容============================================
    //        msg = testTextMessage();
    msg = testScanHasSubEventReqMsg();
    msg = testJsonCmd();
    // ================================================================设置内容============================================
    post.setEntity(new StringEntity(msg, "UTF-8"));

    //        System.out.println("1_-------------------------------------------------------------");
    //        System.out.println(URLEncoder.encode(msg, "UTF-8"));
    //        System.out.println(URLEncoder.encode(msg, "ISO8859-1"));
    //        System.out.println(URLEncoder.encode(msg, "GBK"));
    //        System.out.println("2_-------------------------------------------------------------");
    //        System.out.println(URLDecoder.decode(URLEncoder.encode(msg, "ISO8859-1")));
    //        System.out.println("3_-------------------------------------------------------------");

    // 返回
    HttpResponse response = client.execute(post);
    int statusCode = response.getStatusLine().getStatusCode();

    System.out.println("statusCode:" + statusCode);
    /*4 判断访问的状态码*/
    if (statusCode != HttpStatus.SC_OK) {
      System.err.println("Method failed: ");
      return;
    }
    System.out.println("Response success~...");
    String contend = EntityUtils.toString(response.getEntity());
    System.out.println(contend);
  }