Search in sources :

Example 71 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project SneakerBot by Penor.

the class Supreme method checkout.

public boolean checkout(String variant) {
    HttpPost request = new HttpPost("https://www.supremenewyork.com/checkout.json");
    HttpResponse response = null;
    request.setHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_3 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Mobile/13G34");
    request.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    request.setHeader("Accept-Encoding", "gzip, deflate, sdch");
    request.setHeader("Accept-Language", "en-US,en;q=0.8");
    request.setHeader("Connection", "keep-alive");
    request.setHeader("Host", "www.supremenewyork.com");
    request.setHeader("Upgrade-Insecure-Requests", "1");
    List<NameValuePair> data = generateCheckout(variant);
    try {
        request.setEntity(new UrlEncodedFormEntity(data));
        response = client.execute(request);
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) result.append(line);
        in.close();
        print(result.toString());
        if (response.getStatusLine().getStatusCode() == 200) {
            JSONObject checkoutJson = new JSONObject(result.toString());
            if (!checkoutJson.getString("status").toLowerCase().equals("failed")) {
                itemCarted = true;
                return true;
            } else
                print("Checkout status: " + checkoutJson.getString("status"));
        } else
            print("Status Code: " + response.getStatusLine().getStatusCode() + " Body: " + result.toString());
        // sleep random time 1.5-3 secs
        sleep(new Random().nextInt((int) (3500L - 1500L) + 1) + 1500L);
    } catch (Exception e) {
        if (debug)
            e.printStackTrace();
        else {
            String name = e.getClass().getName();
            if (!name.contains("SocketTimeoutException"))
                print("[Exception - addToCart(productId, variant)] -> " + name);
        }
    } finally {
        if (request != null)
            request.releaseConnection();
        try {
            if (response != null && response.getEntity() != null)
                EntityUtils.consume(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return false;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) Random(java.util.Random) BufferedReader(java.io.BufferedReader) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 72 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project gocd by gocd.

the class HttpServiceTest method shouldSetTheAcceptHeaderWhilePostingProperties.

@Test
public void shouldSetTheAcceptHeaderWhilePostingProperties() throws Exception {
    HttpPost post = mock(HttpPost.class);
    when(httpClientFactory.createPost("url")).thenReturn(post);
    CloseableHttpResponse response = mock(CloseableHttpResponse.class);
    when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
    when(httpClient.execute(post)).thenReturn(response);
    ArgumentCaptor<UrlEncodedFormEntity> entityCaptor = ArgumentCaptor.forClass(UrlEncodedFormEntity.class);
    service.postProperty("url", "value");
    verify(post).setHeader("Confirm", "true");
    verify(post).setEntity(entityCaptor.capture());
    UrlEncodedFormEntity expected = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("value", "value")));
    UrlEncodedFormEntity actual = entityCaptor.getValue();
    assertEquals(IOUtils.toString(expected.getContent()), IOUtils.toString(actual.getContent()));
    assertEquals(expected.getContentLength(), expected.getContentLength());
    assertEquals(expected.getContentType(), expected.getContentType());
    assertEquals(expected.getContentEncoding(), expected.getContentEncoding());
    assertEquals(expected.isChunked(), expected.isChunked());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicStatusLine(org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Example 73 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project 9GAG by Mixiaoxiao.

the class MxxHttpUtil method doHttpPost.

/**
 * Op Http post request , "404error" response if failed
 *
 * @param url
 * @param map
 *            Values to request
 * @return
 */
public static String doHttpPost(String url, HashMap<String, String> map) {
    HttpClient client = new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT);
    HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT);
    ConnManagerParams.setTimeout(client.getParams(), TIMEOUT);
    HttpPost post = new HttpPost(url);
    post.setHeaders(headers);
    String result = "ERROR";
    ArrayList<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
    if (map != null) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue());
            pairList.add(pair);
        }
    }
    try {
        HttpEntity entity = new UrlEncodedFormEntity(pairList, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity(), "UTF-8");
        } else {
            result = EntityUtils.toString(response.getEntity(), "UTF-8") + response.getStatusLine().getStatusCode() + "ERROR";
        }
    } catch (ConnectTimeoutException e) {
        result = "TIMEOUTERROR";
    } catch (Exception e) {
        result = "OTHERERROR";
        e.printStackTrace();
    }
    return result;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HashMap(java.util.HashMap) Map(java.util.Map) ConnectTimeoutException(org.apache.http.conn.ConnectTimeoutException)

Example 74 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project dianping-open-sdk by dianping.

the class RequestUtils method requestAccessToken.

/**
     * 
     * 请求AccessToken<p>
     *
     * Author xiaopeng.li, 2013-12-26
     * @since DPOAuth2ClientDemo 2.0
     *
     * @param code
     * @param state
     * @return
     */
public static String requestAccessToken(String code, String state) {
    DefaultHttpClient client = new DefaultHttpClient();
    List<BasicNameValuePair> paramPair = new ArrayList<BasicNameValuePair>();
    paramPair.add(new BasicNameValuePair("client_id", Constants.APP_KEY));
    paramPair.add(new BasicNameValuePair("grant_type", "authorization_code"));
    paramPair.add(new BasicNameValuePair("scope", "user_info_read"));
    paramPair.add(new BasicNameValuePair("redirect_uri", Constants.DEFAULT_REDIR_URI));
    paramPair.add(new BasicNameValuePair("client_secret", Constants.APP_SECRET));
    paramPair.add(new BasicNameValuePair("code", code));
    paramPair.add(new BasicNameValuePair("state", state));
    try {
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramPair, UTF8);
        HttpPost post = new HttpPost(Constants.TOKEN_URL);
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        int result = response.getStatusLine().getStatusCode();
        StringBuilder stringBuilder = new StringBuilder();
        if (result == 200) {
            InputStream in = response.getEntity().getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, UTF8));
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuilder.append(line).append("\n");
                line = bufferedReader.readLine();
            }
        }
        return stringBuilder.toString();
    } catch (Exception e) {
        return e.getMessage();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BufferedReader(java.io.BufferedReader)

Example 75 with UrlEncodedFormEntity

use of org.apache.http.client.entity.UrlEncodedFormEntity in project opennms by OpenNMS.

the class RequestTracker method postEdit.

public Long postEdit(final HttpPost post, final String content, final Pattern pattern) throws RequestTrackerException {
    String rtTicketNumber = null;
    final List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("user", m_user));
    params.add(new BasicNameValuePair("pass", m_password));
    params.add(new BasicNameValuePair("content", content));
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, StandardCharsets.UTF_8);
    post.setEntity(entity);
    CloseableHttpResponse response = null;
    try {
        response = getClientWrapper().execute(post);
        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode != HttpStatus.SC_OK) {
            throw new RequestTrackerException("Received a non-200 response code from the server: " + responseCode);
        } else {
            final String in = EntityUtils.toString(response.getEntity());
            final Matcher matcher = pattern.matcher(in);
            if (matcher.find()) {
                rtTicketNumber = matcher.group(1);
            } else {
                LOG.debug("did not get ticket ID from response when posting to {}", post);
            }
        }
    } catch (final Exception e) {
        LOG.error("Failure attempting to update ticket.", e);
        throw new RequestTrackerException(e);
    } finally {
        getClientWrapper().close(response);
    }
    if (rtTicketNumber == null) {
        return null;
    }
    return Long.valueOf(rtTicketNumber);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) Matcher(java.util.regex.Matcher) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException)

Aggregations

UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)134 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)111 HttpPost (org.apache.http.client.methods.HttpPost)105 NameValuePair (org.apache.http.NameValuePair)100 ArrayList (java.util.ArrayList)96 HttpResponse (org.apache.http.HttpResponse)74 IOException (java.io.IOException)47 HttpEntity (org.apache.http.HttpEntity)40 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)31 ClientProtocolException (org.apache.http.client.ClientProtocolException)27 UnsupportedEncodingException (java.io.UnsupportedEncodingException)26 HttpClient (org.apache.http.client.HttpClient)20 Test (org.junit.Test)20 HttpGet (org.apache.http.client.methods.HttpGet)19 Map (java.util.Map)18 JSONObject (org.json.JSONObject)18 TestHttpClient (io.undertow.testutils.TestHttpClient)14 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)14 HashMap (java.util.HashMap)13 Header (org.apache.http.Header)13