Example usage for org.apache.http.client.methods HttpPost setEntity

List of usage examples for org.apache.http.client.methods HttpPost setEntity

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpPost setEntity.

Prototype

public void setEntity(final HttpEntity entity) 

Source Link

Usage

From source file:App.App.java

public static void main(String[] args) throws IOException {
    File inFile = new File("C:\\Data\\LogTest.java");
    FileInputStream fis = new FileInputStream(inFile);
    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
    HttpPost httppost = new HttpPost("http://localhost:8080/rest_j2ee/rest/files/upload");
    MultipartEntity entity = new MultipartEntity();
    entity.addPart("file", new InputStreamBody(fis, inFile.getName()));
    httppost.setEntity(entity);
    HttpResponse response = httpclient.execute(httppost);
    int statusCode = response.getStatusLine().getStatusCode();
    HttpEntity responseEntity = response.getEntity();
    String responseString = EntityUtils.toString(responseEntity, "UTF-8");
    System.out.println("[" + statusCode + "] " + responseString);
}

From source file:org.vuphone.vandyupon.test.EventMetaRequestTester.java

public static void main(String[] args) {
    HttpClient c = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/vandyupon/events/");
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    String params = "type=eventmetarequest&id=2&resp=xml";
    post.setEntity(new ByteArrayEntity(params.toString().getBytes()));

    try {//ww w.  j  ava2  s.  com
        HttpResponse resp = c.execute(post);
        resp.getEntity().writeTo(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:isc_415_practica_1.ISC_415_Practica_1.java

/**
 * @param args the command line arguments
 */// w ww . j av  a 2 s. c o m
public static void main(String[] args) {
    String urlString;
    Scanner input = new Scanner(System.in);
    Document doc;

    try {
        urlString = input.next();
        if (urlString.equals("servlet")) {
            urlString = "http://localhost:8084/ISC_415_Practica1_Servlet/client";
        }
        urlString = urlString.contains("http://") || urlString.contains("https://") ? urlString
                : "http://" + urlString;
        doc = Jsoup.connect(urlString).get();
    } catch (Exception ex) {
        System.out.println("El URL ingresado no es valido.");
        return;
    }

    ArrayList<NameValuePair> formInputParams;
    formInputParams = new ArrayList<>();
    String[] plainTextDoc = new TextNode(doc.html(), "").getWholeText().split("\n");
    System.out.println(String.format("Nmero de lineas del documento: %d", plainTextDoc.length));
    System.out.println(String.format("Nmero de p tags: %d", doc.select("p").size()));
    System.out.println(String.format("Nmero de img tags: %d", doc.select("img").size()));
    System.out.println(String.format("Nmero de form tags: %d", doc.select("form").size()));

    Integer index = 1;

    ArrayList<NameValuePair> urlParameters = new ArrayList<>();
    for (Element e : doc.select("form")) {
        System.out.println(String.format("Form %d: Nmero de Input tags %d", index, e.select("input").size()));
        System.out.println(e.select("input"));

        for (Element formInput : e.select("input")) {
            if (formInput.attr("id") != null && formInput.attr("id") != "") {
                urlParameters.add(new BasicNameValuePair(formInput.attr("id"), "PRACTICA1"));
            } else if (formInput.attr("name") != null && formInput.attr("name") != "") {
                urlParameters.add(new BasicNameValuePair(formInput.attr("name"), "PRACTICA1"));
            }
        }

        index++;
    }

    if (!urlParameters.isEmpty()) {
        try {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters, Consts.UTF_8);
            HttpPost httpPost = new HttpPost(urlString);
            httpPost.setHeader("User-Agent", USER_AGENT);
            httpPost.setEntity(entity);
            HttpResponse response = httpclient.execute(httpPost);
            System.out.println(response.getStatusLine());
        } catch (IOException ex) {
            Logger.getLogger(ISC_415_Practica_1.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

From source file:com.client.SoapClientApache.java

public static void main(String[] args) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {/* w ww  . j a  v a2s .  c o  m*/
        HttpGet httpGet = new HttpGet("http://google.com/");
        CloseableHttpResponse response1 = httpclient.execute(httpGet);
        // The underlying HTTP connection is still held by the response object
        // to allow the response content to be streamed directly from the network socket.
        // In order to ensure correct deallocation of system resources
        // the user MUST either fully consume the response content  or abort request
        // execution by calling CloseableHttpResponse#close().

        try {
            System.out.println("Response for http get");
            System.out.println(response1.getStatusLine());
            HttpEntity entity1 = response1.getEntity();
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity1);
        } finally {
            response1.close();
        }

        File file = new File("temConvertRq.xml");
        File fileOutXml = new File("temConvertRs.xml");
        fileOutXml.createNewFile();

        System.out.println("Before file entity");
        FileEntity entity = new FileEntity(file, ContentType.create("text/xml", "UTF-8"));

        System.out.println("Before http post");
        HttpPost httpPost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");

        httpPost.setEntity(entity);
        System.out.println("Entity set");
        //Add headers
        httpPost.addHeader("SOAPAction", "http://www.w3schools.com/webservices/CelsiusToFahrenheit");
        httpPost.addHeader("Host", "www.w3schools.com");
        httpPost.addHeader("Content-Type", "text/xml; charset=utf-8");
        //httpPost.addHeader("Content-Length", "length");

        System.out.println("after headers added");
        CloseableHttpResponse response2 = httpclient.execute(httpPost);

        try {
            System.out.println("before getting the response status");
            System.out.println("Response Status= " + response2.getStatusLine());
            HttpEntity entity2 = response2.getEntity();
            String outXml = EntityUtils.toString(entity2);
            System.out.println("XMl Response!!!!!!!!! = " + outXml);

            //                entity2.writeTo(System.out);

            //System.out.println("R
            // do something useful with the response body
            // and ensure it is fully consumed
            EntityUtils.consume(entity2);

            FileUtils.writeStringToFile(fileOutXml, outXml, "UTF-8");

        } finally {
            response2.close();
        }
    } finally {
        httpclient.close();
    }
}

From source file:org.vuphone.vandyupon.test.EventPostTester.java

public static void main(String[] args) {
    HttpClient c = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/vandyupon/events/");
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    String params = "type=eventratingrequest&id=1&comments=true&numcom=10";
    post.setEntity(new ByteArrayEntity(params.toString().getBytes()));

    try {/*from w w w  .java2s.  com*/
        HttpResponse resp = c.execute(post);
        resp.getEntity().writeTo(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:TestRESTPost12.java

public static void main(String[] p) throws Exception {
    String strurl = "http://localhost:8080/testnewmaven8/webresources/service/post";

    //StringEntity str=new StringEntity("<a>hello post</a>",ContentType.create("application/xml" , Consts.UTF_8));

    ////w w w.j a v a 2  s .  c  o  m
    StringEntity str = new StringEntity("hello post");
    str.setContentType("APPLICATION/xml");

    CloseableHttpClient httpclient = HttpClients.createDefault();

    HttpPost httppost = new HttpPost(strurl);
    httppost.addHeader("Accept", "application/xml charset=UTF-8");
    //httppost.addHeader("content_type", "application/xml, multipart/related");
    httppost.setEntity(str);

    CloseableHttpResponse response = httpclient.execute(httppost);
    // try
    //{
    int statuscode = response.getStatusLine().getStatusCode();
    if (statuscode != 200) {
        System.out.println("http error occured=" + statuscode);
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    while (br.readLine() != null) {
        System.out.println(br.readLine());
    }
    // }
    /*catch(Exception e)
    {
        System.out.println("exception :"+e);
    }*/

    //httpclient.close();

}

From source file:RestPostClient.java

public static void main(String[] args) throws Exception {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost postRequest = new HttpPost("http://localhost:10080/example/json/product/post");

    StringEntity input = new StringEntity("{\"qty\":100,\"name\":\"iPad 4\"}");
    input.setContentType("application/json");
    postRequest.setEntity(input);

    HttpResponse response = httpClient.execute(postRequest);

    //if (response.getStatusLine().getStatusCode() != 201) {
    //   throw new RuntimeException("Failed : HTTP error code : "
    //      + response.getStatusLine().getStatusCode());
    //}//w  ww.j  a v a 2 s. c o  m

    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }

    httpClient.getConnectionManager().shutdown();
}

From source file:org.vuphone.vandyupon.test.EventRatingPostTest.java

public static void main(String[] args) {
    HttpClient c = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/vandyupon/events/");
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    String params = "type=eventratingpost&event=1&user=chris&comment=awesome&value=1&resp=xml";
    post.setEntity(new ByteArrayEntity(params.toString().getBytes()));

    try {//from  ww  w. j a  va2s. c o  m
        HttpResponse resp = c.execute(post);
        resp.getEntity().writeTo(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.vuphone.vandyupon.test.EventRequestTester.java

public static void main(String[] args) {
    HttpClient c = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/vandyupon/events/");
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    String params = "type=eventrequest&lat=36.1437&lon=-86.8046&dist=100&resp=xml&userid=chris";
    post.setEntity(new ByteArrayEntity(params.toString().getBytes()));

    try {//  w  w  w  .  j ava2 s  .  co  m
        HttpResponse resp = c.execute(post);
        resp.getEntity().writeTo(System.out);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:org.wso2.carbon.sample.atmstats.ATMTransactionStatsClient.java

public static void main(String[] args) throws XMLStreamException {
    System.out.println(xmlMsgs.get(1));
    System.out.println(xmlMsgs.get(2));
    System.out.println(xmlMsgs.get(3));
    KeyStoreUtil.setTrustStoreParams();/*from  ww  w  .j  a  va2 s  .com*/
    String url = args[0];
    String username = args[1];
    String password = args[2];

    HttpClient httpClient = new SystemDefaultHttpClient();

    try {
        HttpPost method = new HttpPost(url);

        for (String xmlElement : xmlMsgs) {
            StringEntity entity = new StringEntity(xmlElement);
            method.setEntity(entity);
            if (url.startsWith("https")) {
                processAuthentication(method, username, password);
            }
            httpClient.execute(method).getEntity().getContent().close();
        }
        Thread.sleep(500); // We need to wait some time for the message to be sent

    } catch (Throwable t) {
        t.printStackTrace();
    }
}