use of org.apache.http.client.entity.UrlEncodedFormEntity in project webmagic by code4craft.
the class HttpClientDownloader method addFormParams.
private RequestBuilder addFormParams(RequestBuilder requestBuilder, NameValuePair[] nameValuePair, Map<String, String> params) {
List<NameValuePair> allNameValuePair = new ArrayList<NameValuePair>();
if (nameValuePair != null && nameValuePair.length > 0) {
allNameValuePair = Arrays.asList(nameValuePair);
}
if (params != null) {
for (String key : params.keySet()) {
allNameValuePair.add(new BasicNameValuePair(key, params.get(key)));
}
}
requestBuilder.setEntity(new UrlEncodedFormEntity(allNameValuePair, Charset.forName("utf8")));
return requestBuilder;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project PneumaticCraft by MineMaarten.
the class PastebinHandler method putInternal.
public String putInternal(String contents) {
HttpPost httppost = new HttpPost("http://pastebin.com/api/api_post.php");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("api_dev_key", DEV_KEY));
params.add(new BasicNameValuePair("api_paste_code", contents));
params.add(new BasicNameValuePair("api_option", "paste"));
if (isLoggedIn())
params.add(new BasicNameValuePair("api_user_key", userKey));
try {
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
return IOUtils.toString(instream, "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project carat by amplab.
the class JsonParser method getJSONFromUrl.
public String getJSONFromUrl(String url) {
InputStream inputStream = null;
String result = null;
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
// Set up HTTP post
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// Read content & Log
inputStream = httpEntity.getContent();
} catch (UnknownHostException e0) {
Log.d("JsonParser", "Unable to connect to the statstics server (no Internet on the device! is Wifi or mobile data on?), " + e0.toString());
return "";
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
return "";
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
return "";
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
return "";
} catch (IOException e4) {
Log.e("IOException", e4.toString());
return "";
}
// Convert response to string using String Builder
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
}
return result;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project ninja by ninjaframework.
the class NinjaTestBrowser method makePostRequestWithFormParameters.
public String makePostRequestWithFormParameters(String url, Map<String, String> headers, Map<String, String> formParameters) {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
HttpPost postRequest = new HttpPost(url);
if (headers != null) {
// add all headers
for (Entry<String, String> header : headers.entrySet()) {
postRequest.addHeader(header.getKey(), header.getValue());
}
}
// add form parameters:
List<BasicNameValuePair> formparams = new ArrayList<>();
if (formParameters != null) {
for (Entry<String, String> parameter : formParameters.entrySet()) {
formparams.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
}
}
// encode form parameters and add
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams);
postRequest.setEntity(entity);
HttpResponse response;
response = httpClient.execute(postRequest);
br = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String output;
while ((output = br.readLine()) != null) {
sb.append(output);
}
postRequest.releaseConnection();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
LOG.error("Failed to close resource", e);
}
}
}
return sb.toString();
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project AndEngine by nicolasgramlich.
the class LevelStatsDBConnector method submitAsync.
public void submitAsync(final int pLevelID, final boolean pSolved, final int pSecondsElapsed, final Callback<Boolean> pCallback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
/* Create a new HttpClient and Post Header. */
final HttpClient httpClient = new DefaultHttpClient();
final HttpPost httpPost = new HttpPost(LevelStatsDBConnector.this.mSubmitURL);
/* Append POST data. */
final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("level_id", String.valueOf(pLevelID)));
nameValuePairs.add(new BasicNameValuePair("solved", (pSolved) ? "1" : "0"));
nameValuePairs.add(new BasicNameValuePair("secondsplayed", String.valueOf(pSecondsElapsed)));
nameValuePairs.add(new BasicNameValuePair("player_id", String.valueOf(LevelStatsDBConnector.this.mPlayerID)));
nameValuePairs.add(new BasicNameValuePair("secret", String.valueOf(LevelStatsDBConnector.this.mSecret)));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
/* Execute HTTP Post Request. */
final HttpResponse httpResponse = httpClient.execute(httpPost);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
final String response = StreamUtils.readFully(httpResponse.getEntity().getContent());
if (response.equals("<success/>")) {
if (pCallback != null) {
pCallback.onCallback(true);
}
} else {
if (pCallback != null) {
pCallback.onCallback(false);
}
}
} else {
if (pCallback != null) {
pCallback.onCallback(false);
}
}
} catch (final IOException e) {
Debug.e(e);
if (pCallback != null) {
pCallback.onCallback(false);
}
}
}
}).start();
}
Aggregations