use of org.apache.http.client.entity.UrlEncodedFormEntity in project mobile-android by photo.
the class ApiBase method createHttpRequest.
/**
* Create a HttpUriRequest out of a ApiRequest object.
*
* @param request the ApiRequest for which a HttpUriRequest should be
* created
* @param baseUrl the base server url
* @param listener Progress Listener with callback on progress
* @return HttpUriRequest object which will do the request as described in
* ApiRequest
* @throws UnsupportedEncodingException
*/
private HttpUriRequest createHttpRequest(ApiRequest request, String baseUrl, ProgressListener listener) throws UnsupportedEncodingException {
HttpUriRequest httpRequest = null;
switch(request.getMethod()) {
case ApiRequest.GET:
httpRequest = new HttpGet(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
break;
case ApiRequest.POST:
httpRequest = new HttpPost(baseUrl + request.getPath());
HttpPost httpPost = ((HttpPost) httpRequest);
if (request.isMime()) {
// TODO use the multipart when possible (currently server
// handles it wrong)
// HttpEntity entity = createMultipartEntity(request);
// TODO remove this when doing correct multipart
httpRequest = new HttpPost(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
httpPost = ((HttpPost) httpRequest);
HttpEntity entity = createFileOnlyMultipartEntity(request);
if (listener != null) {
httpPost.setEntity(new HttpEntityWithProgress(entity, listener, httpPost));
} else {
httpPost.setEntity(entity);
}
} else {
httpPost.setEntity(new UrlEncodedFormEntity(request.getParameters(), HTTP.UTF_8));
}
break;
case ApiRequest.PUT:
httpRequest = new HttpPut(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
break;
case ApiRequest.DELETE:
httpRequest = new HttpDelete(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
break;
}
for (NameValuePair pair : request.getHeaders()) {
request.addHeader(pair.getName(), pair.getValue());
}
return httpRequest;
}
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 pinpoint by naver.
the class ClosableAsyncHttpClientIT method test.
@Test
public void test() throws Exception {
CloseableHttpAsyncClient httpClient = HttpAsyncClients.custom().useSystemProperties().build();
httpClient.start();
try {
HttpPost httpRequest = new HttpPost("http://www.naver.com/");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("param1", "value1"));
httpRequest.setEntity(new UrlEncodedFormEntity(params, Consts.UTF_8.name()));
Future<HttpResponse> responseFuture = httpClient.execute(httpRequest, null);
HttpResponse response = (HttpResponse) responseFuture.get();
if ((response != null) && (response.getEntity() != null)) {
EntityUtils.consume(response.getEntity());
}
} finally {
httpClient.close();
}
PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
verifier.printCache();
verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", CloseableHttpAsyncClient.class.getMethod("execute", HttpUriRequest.class, FutureCallback.class)));
verifier.verifyTrace(async(event("HTTP_CLIENT_4", Class.forName("org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl").getMethod("start"), null, null, "www.naver.com", annotation("http.url", "http://www.naver.com/"), annotation("http.entity", "param1=value1")), event("ASYNC", "Asynchronous Invocation"), event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("completed", Object.class))));
verifier.verifyTrace(event("HTTP_CLIENT_4_INTERNAL", BasicFuture.class.getMethod("get")));
verifier.verifyTraceCount(0);
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project pinpoint by naver.
the class HttpRemoteService method createPost.
private HttpPost createPost(String url, MultiValueMap<String, String> params) throws UnsupportedEncodingException {
HttpPost post = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Map.Entry<String, List<String>> entry : params.entrySet()) {
String key = entry.getKey();
for (String value : entry.getValue()) {
nvps.add(new BasicNameValuePair(key, value));
}
}
post.setEntity(new UrlEncodedFormEntity(nvps));
return post;
}
use of org.apache.http.client.entity.UrlEncodedFormEntity in project DrupalCloud by INsReady.
the class JSONServerClient method call.
/**
* Generic request
*
* @param method
* Request name
* @param parameters
* Parameters
* @return result string
*/
public String call(String method, BasicNameValuePair[] parameters) throws ServiceNotAvailableException {
String sessid = this.getSessionID();
mPairs.clear();
String nonce = Integer.toString(new Random().nextInt());
Mac hmac;
try {
hmac = Mac.getInstance(JSONServerClient.mALGORITHM);
final Long timestamp = new Date().getTime() / 100;
final String time = timestamp.toString();
hmac.init(new SecretKeySpec(JSONServerClient.mAPI_KEY.getBytes(), JSONServerClient.mALGORITHM));
String message = time + ";" + JSONServerClient.mDOMAIN + ";" + nonce + ";" + method;
hmac.update(message.getBytes());
String hmac_value = new String(Hex.encodeHex(hmac.doFinal()));
mPairs.add(new BasicNameValuePair("hash", hmac_value));
mPairs.add(new BasicNameValuePair("domain_name", JSONServerClient.mDOMAIN));
mPairs.add(new BasicNameValuePair("domain_time_stamp", time));
mPairs.add(new BasicNameValuePair("nonce", nonce));
mPairs.add(new BasicNameValuePair("method", method));
mPairs.add(new BasicNameValuePair("api_key", JSONServerClient.mAPI_KEY));
mPairs.add(new BasicNameValuePair("sessid", sessid));
for (int i = 0; i < parameters.length; i++) {
mPairs.add(parameters[i]);
}
mSERVER.setEntity(new UrlEncodedFormEntity(mPairs));
HttpResponse response = mClient.execute(mSERVER);
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result = br.readLine();
JSONObject jso;
jso = new JSONObject(result);
boolean error = jso.getBoolean("#error");
if (error) {
String errorMsg = jso.getString("#data");
throw new ServiceNotAvailableException(this, errorMsg);
}
return result;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
throw new ServiceNotAvailableException("Remote server is not available");
}
return null;
}
Aggregations