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;
}
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());
}
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;
}
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();
}
}
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);
}
Aggregations