List of usage examples for org.apache.http.client.methods HttpPost HttpPost
public HttpPost(final String uri)
From source file:org.wso2.carbon.integration.test.client.PizzaOrderClient.java
public static void sendPizzaOrder(String url) { KeyStoreUtil.setTrustStoreParams();/*from w w w. ja va2 s .c om*/ HttpClient httpClient = new SystemDefaultHttpClient(); try { HttpPost method = new HttpPost(url); String[] xmlElements = new String[] { "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0023</mypizza:OrderNo>\n" + " <mypizza:Type>PEPPERONI</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>2</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0024</mypizza:OrderNo>\n" + " <mypizza:Type>CHEESE</mypizza:Type>\n" + " <mypizza:Size>M</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Henry Clock</mypizza:Contact>\n" + " <mypizza:Address>2CYL Morris Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0025</mypizza:OrderNo>\n" + " <mypizza:Type>SEAFOOD</mypizza:Type>\n" + " <mypizza:Size>S</mypizza:Size>\n" + " <mypizza:Quantity>4</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>22RE Robinwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>CHICKEN</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Contact>Alis Miranda</mypizza:Contact>\n" + " <mypizza:Address>779 Burl Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>VEGGIE</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>" }; String[] batchedXmlElements = new String[] { "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0023</mypizza:OrderNo>\n" + " <mypizza:Type>PEPPERONI</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>2</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0024</mypizza:OrderNo>\n" + " <mypizza:Type>CHEESE</mypizza:Type>\n" + " <mypizza:Size>M</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Henry Clock</mypizza:Contact>\n" + " <mypizza:Address>2CYL Morris Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0025</mypizza:OrderNo>\n" + " <mypizza:Type>SEAFOOD</mypizza:Type>\n" + " <mypizza:Size>S</mypizza:Size>\n" + " <mypizza:Quantity>4</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>22RE Robinwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>", "<mypizza:PizzaOrderStream xmlns:mypizza=\"http://samples.wso2.org/\">\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>CHICKEN</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>Alis Miranda</mypizza:Contact>\n" + " <mypizza:Address>779 Burl Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + " <mypizza:PizzaOrder>\n" + " <mypizza:OrderNo>0026</mypizza:OrderNo>\n" + " <mypizza:Type>VEGGIE</mypizza:Type>\n" + " <mypizza:Size>L</mypizza:Size>\n" + " <mypizza:Quantity>1</mypizza:Quantity>\n" + " <mypizza:Contact>James Mark</mypizza:Contact>\n" + " <mypizza:Address>29BX Finchwood Ave, Clovis, CA 93611</mypizza:Address>\n" + " </mypizza:PizzaOrder>\n" + "</mypizza:PizzaOrderStream>" }; int i = 0; for (String xmlElement : xmlElements) { StringEntity entity = new StringEntity(xmlElement); method.setEntity(entity); httpClient.execute(method).getEntity().getContent().close(); System.out.println("Sent event no :" + i++); } Thread.sleep(500); // We need to wait some time for the message to be sent } catch (Throwable t) { t.printStackTrace(); Assert.fail("Exception: " + t.getMessage()); } }
From source file:manager.computerVisionManager.java
private static void getJson(String path) { System.out.println("Get Description from https://westus.api.cognitive.microsoft.com/vision/v1.0/describe"); try {//from w w w .j a va 2s . com URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/vision/v1.0/describe"); builder.setParameter("maxCandidates", "1"); URI uri = builder.build(); HttpPost request = new HttpPost(uri); request.setHeader("Content-Type", "application/json"); request.setHeader("Ocp-Apim-Subscription-Key", "d7f6ef12e41c4f8c8e72d12a890fa703"); // Request body StringEntity reqEntity = new StringEntity("{\"url\":\"" + path + "\"}"); System.out.println("Request String: " + reqEntity.toString()); request.setEntity(reqEntity); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { String respuesta = EntityUtils.toString(entity); JSONParser lector = new JSONParser(); StringReader SR = new StringReader(respuesta); try { JSONObject temp = (JSONObject) lector.parse(SR); json = (JSONObject) temp.get("description"); } catch (org.json.simple.parser.ParseException e) { System.err.println(e.getMessage()); } } } catch (IOException | URISyntaxException | ParseException e) { System.err.println(e.getMessage()); } }
From source file:Extras.JSON.java
public static HttpResponse request(String URL) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(URL); HttpResponse response = null;// www.j ava 2 s. c o m try { response = httpclient.execute(httppost); } catch (Exception e) { } return response; }
From source file:org.roda.core.util.RESTClientUtility.java
public static <T extends Serializable> T sendPostRequest(T element, Class<T> elementClass, String url, String resource, String username, String password) throws RODAException { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); String basicAuthToken = new String(Base64.encode((username + ":" + password).getBytes())); HttpPost httpPost = new HttpPost(url + resource); httpPost.setHeader("Authorization", "Basic " + basicAuthToken); httpPost.addHeader("content-type", "application/json"); httpPost.addHeader("Accept", "application/json"); try {/*from w ww . j av a 2 s . c om*/ httpPost.setEntity(new StringEntity(JsonUtils.getJsonFromObject(element))); HttpResponse response; response = httpClient.execute(httpPost); HttpEntity responseEntity = response.getEntity(); int responseStatusCode = response.getStatusLine().getStatusCode(); if (responseStatusCode == 201) { return JsonUtils.getObjectFromJson(responseEntity.getContent(), elementClass); } else { throw new RODAException("POST request response status code: " + responseStatusCode); } } catch (IOException e) { throw new RODAException("Error sending POST request", e); } }
From source file:org.opencastproject.remotetest.server.resource.SearchResources.java
public static HttpResponse add(TrustedHttpClient client, String mediapackage) throws Exception { HttpPost post = new HttpPost(getServiceUrl() + "add"); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("mediapackage", mediapackage)); post.setEntity(new UrlEncodedFormEntity(params)); return client.execute(post); }
From source file:xj.property.ums.common.NetworkUitlity.java
public static MyMessage post(String url, String data) { // TODO Auto-generated method stub CommonUtil.printLog("ums", url); String returnContent = ""; MyMessage message = new MyMessage(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {/*from w ww. j ava2 s .c o m*/ StringEntity se = new StringEntity(data, HTTP.UTF_8); CommonUtil.printLog("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); CommonUtil.printLog("ums", status + ""); String returnXML = EntityUtils.toString(response.getEntity()); returnContent = URLDecoder.decode(returnXML); switch (status) { case 200: message.setFlag(true); message.setMsg(returnContent); break; default: Log.e("error", status + returnContent); message.setFlag(false); message.setMsg(returnContent); break; } } catch (Exception e) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("err", e.toString()); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { e1.printStackTrace(); } } CommonUtil.printLog("UMSAGENT", message.getMsg()); return message; }
From source file:com.wbtech.ums.common.NetworkUitlity.java
public static MyMessage post(String url, String data) { // TODO Auto-generated method stub CommonUtil.printLog("ums", url); String returnContent = ""; MyMessage message = new MyMessage(); HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); try {// w w w . j ava2s . co m StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8); CommonUtil.printLog("postdata", "content=" + data); se.setContentType("application/x-www-form-urlencoded"); httppost.setEntity(se); HttpResponse response = httpclient.execute(httppost); int status = response.getStatusLine().getStatusCode(); CommonUtil.printLog("ums", status + ""); String returnXML = EntityUtils.toString(response.getEntity()); returnContent = URLDecoder.decode(returnXML); switch (status) { case 200: message.setFlag(true); message.setMsg(returnContent); break; default: Log.e("error", status + returnContent); message.setFlag(false); message.setMsg(returnContent); break; } } catch (Exception e) { JSONObject jsonObject = new JSONObject(); try { jsonObject.put("err", e.toString()); returnContent = jsonObject.toString(); message.setFlag(false); message.setMsg(returnContent); } catch (JSONException e1) { e1.printStackTrace(); } } CommonUtil.printLog("UMSAGENT", message.getMsg()); return message; }
From source file:uk.ac.ebi.atlas.utils.HttpRequest.java
public static InputStream httpPost(org.apache.http.client.HttpClient httpClient, String url, List<? extends NameValuePair> params) throws IOException { HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { return response.getEntity().getContent(); }/*from w ww .ja v a 2s .co m*/ throw new IOException( "Server returned invalid response: [status_code = " + statusCode + "; url = " + url + "]"); }
From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromFoursquareApi.java
public static String getAddress(double latitude, double longitude) { String strAddress = ""; String point = latitude + "," + longitude; String url = "https://api.foursquare.com/v2/venues/search?ll=" + latitude + "," + longitude + "&oauth_token=5TJR4WQZSOW0ZWTE4ENMXKO3Y415252GITEMRPQIVPMEGCYK&v=20120723&limit=1&accept-language=en"; HttpPost httppost = new HttpPost(url); HttpClient httpclient = new DefaultHttpClient(); String result = ""; try {/*from w ww . ja v a 2s .co m*/ HttpResponse response = httpclient.execute(httppost); HttpEntity resEntityGet = response.getEntity(); if (resEntityGet != null) { result = EntityUtils.toString(resEntityGet); } } catch (Exception e) { System.out.println("Exception:" + e); } try { /*Keeps the first address form the results*/ if (result != null) { JSONObject json = new JSONObject(result); JSONObject jo = json.getJSONObject("response"); JSONArray ja = jo.getJSONArray("venues"); for (int i = 0; i < 1; i++) { strAddress = ja.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress") .toString(); strAddress = strAddress.replace("},{", ",").replace("[", "").replace("]", "").replace("\"", ""); } } } catch (Exception e) { } try { if (!strAddress.equals("")) { pointAddressMap.put(point, strAddress); FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true); } } catch (IOException ex) { Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex); } return strAddress; }
From source file:com.matze5800.paupdater.JSONfunctions.java
public static JSONObject getJSONfromURL(String url) { InputStream is = null;/* w w w . j a v a2 s. c o m*/ String result = ""; JSONObject jArray = null; //http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } try { jArray = new JSONObject(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jArray; }