// Build the JSON object to pass parameters JSONObject jsonObj = new JSONObject(); jsonObj.put("username", username); jsonObj.put("apikey", apikey); // Create the POST object and add the parameters HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpClient client = new DefaultHttpClient(); HttpResponse response = client.execute(httpPost);
private static HttpPost preparePostRequest(String requestUrl, String sessionId, Map<String, String> params) throws IOException { // Create post request HttpPost postRequest = new HttpPost(requestUrl); StringBuilder stringEntityBuilder = new StringBuilder(); stringEntityBuilder.append(String.format("session.id=%s", sessionId)); for (Map.Entry<String, String> entry : params.entrySet()) { if (stringEntityBuilder.length() > 0) { stringEntityBuilder.append("&"); } stringEntityBuilder.append(String.format("%s=%s", entry.getKey(), entry.getValue())); } StringEntity input = new StringEntity(stringEntityBuilder.toString()); input.setContentType("application/x-www-form-urlencoded"); postRequest.setEntity(input); postRequest.setHeader("X-Requested-With", "XMLHttpRequest"); return postRequest; }
DefaultHttpClient httpClient = new DefaultHttpClient(); String jsonResult = null; HttpPost method = new HttpPost(url); try { if (null != jsonParam) { entity.setContentType("application/json"); method.setEntity(entity);
HttpPost httpPost = new HttpPost(url_src); HttpParams httpParameters = new BasicHttpParams(); httpclient.setParams(httpParameters); StringEntity se = new StringEntity(str,"UTF-8"); se.setContentType("application/json"); httpPost.setEntity(se); httpPost.setHeader("userId", ""+userId);//userID httpPost.setHeader("machineId", machineId);//deviceUserMachineID response = httpclient.execute(httpPost);
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPut putRequest = new HttpPut(URI); StringEntity input = new StringEntity(XML); input.setContentType(CONTENT_TYPE); putRequest.setEntity(input); HttpResponse response = httpClient.execute(putRequest);
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://localhost:8080/TESTINGrestful/rest/polls/comment"); StringEntity input = new StringEntity("<Comment>...</Comment>"); input.setContentType("text/xml"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest);
public String postJson(String url, Object object) { try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity( new ObjectMapper().writeValueAsString(object), "utf-8"); entity.setContentType("application/json; charset=utf-8"); post.setEntity(entity); post.releaseConnection(); // Here we go! return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
HttpPost httppost = new HttpPost(SERVICE_EPR); StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8); se.setContentType("text/xml"); httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8"); httppost.setEntity(se); HttpClient httpclient = new DefaultHttpClient(); BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost); response.put("HTTPStatus",httpResponse.getStatusLine().toString());
HttpPost post = new HttpPost(LOGIN_URL); StringEntity se = new StringEntity(json); se.setContentEncoding("UTF-8"); se.setContentType("application/json"); post.setEntity(se); response = client.execute(post);
mHttpClient = new DefaultHttpClient(); mHttpGet = new HttpRequestWithEntity(url, method); JSONObject mObject = new JSONObject(); for (String[] pair : params) { mObject.put(pair[0], pair[1]); } StringEntity mStringEntity = new StringEntity(mObject.toString()); mStringEntity.setContentEncoding("UTF-8"); mStringEntity.setContentType("application/json"); mHttpGet.setEntity(mStringEntity); HttpResponse mResponse = mHttpClient.execute(mHttpGet);
HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://192.168.192.131/"); try { StringEntity se = new StringEntity( "<aaaLogin inName=\"admin\" inPassword=\"admin123\"/>", HTTP.UTF_8); se.setContentType("text/xml"); httppost.setEntity(se); HttpResponse httpresponse = httpclient.execute(httppost); HttpEntity resEntity = httpresponse.getEntity(); tvData.setText(EntityUtils.toString(resEntity)); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
public String postXml(String url, Object object) { try { httpClient.getParams().setParameter( CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpPost post = new HttpPost(url); StringEntity entity = new StringEntity( new XmlMapper().writeValueAsString(object), "utf-8"); entity.setContentType("application/xml; charset=utf-8"); post.setEntity(entity); post.releaseConnection(); // Here we go! return EntityUtils.toString(httpClient.execute(post).getEntity(), "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
protected void sendJson(final String email, final String pwd) { Thread t = new Thread() { public void run() { Looper.prepare(); //For Preparing Message Pool for the child Thread HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response; JSONObject json = new JSONObject(); try { HttpPost post = new HttpPost(URL); json.put("email", email); json.put("password", pwd); StringEntity se = new StringEntity( json.toString()); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); /*Checking response */ if(response!=null){ InputStream in = response.getEntity().getContent(); //Get the data in the entity } } catch(Exception e) { e.printStackTrace(); createDialog("Error", "Cannot Estabilish Connection"); } Looper.loop(); //Loop in the message queue } }; t.start(); }
StringEntity stringEntity = new StringEntity(jsonArray.toString()); stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING, "UTF-8")); stringEntity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); HttpPost post = new HttpPost(url); post.setEntity(stringEntity); post.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; public class Runner { public static void main(String[] args) { try { HttpClient httpClient = new DefaultHttpClient(); HttpPut putRequest = new HttpPut("http://localhost:8080/HelloFromJersey/input/result"); StringEntity input = new StringEntity("Hello, this is a message from your put client!"); input.setContentType("text/xml"); putRequest.setEntity(input); httpClient.execute(putRequest); httpClient.getConnectionManager().shutdown(); } catch (Exception ex) { ex.printStackTrace(); } } }
String photoID = ""; HttpPost postRequest = new HttpPost( "https://picasaweb.google.com/data/feed/api/user/" + userID + "/albumid/" + albumID + "/photoid/" + photoID); entity.setContentType(new BasicHeader("Content-Type", "application/atom+xml")); postRequest.setEntity(entity); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = httpclient.execute(postRequest);
@Test public void parseParams_shouldParsePostEntitiesIntoParams() throws Exception { HttpPost post = new HttpPost("example.com"); StringEntity entity = new StringEntity("param1=foobar"); entity.setContentType("application/x-www-form-urlencoded"); post.setEntity(entity); Map<String,String> params = ParamsParser.parseParams(post); assertThat(params.get("param1")).isEqualTo("foobar"); }
HttpClient client = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit HttpResponse response; JSONObject json = new JSONObject(); try { HttpPost post = new HttpPost(URL); json.put("user_name", "chintan"); json.put("password", "khetiya"); StringEntity se = new StringEntity( json.toString()); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); post.setEntity(se); response = client.execute(post); /*Checking response */ if(response!=null){ is = response.getEntity().getContent(); //Get the data in the entity } } catch(Exception e) { e.printStackTrace(); createDialog("Error", "Cannot Estabilish Connection"); }
HttpPost post = new HttpPost(url1); StringEntity stringEntity = new StringEntity(json); stringEntity.setContentEncoding("UTF-8"); stringEntity.setContentType("application/json"); post.setEntity(stringEntity); response = client.execute(post);
HttpPut request = new HttpPut("your URL"); JSONStringer json = new JSONStringer() .object() .key("cname").value(name) .key("cmail").value(email) .endObject(); StringEntity entity = new StringEntity(json.toString()); entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8 entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8")); request.setEntity(entity); // Send request to WCF service DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(request);