Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java http request post

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");

// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

if (entity != null) {
    try (InputStream instream = entity.getContent()) {
        // do something useful
    }
}
Comment

How to Implement GET and POST Requests With Java

public static void MyGETRequest() throws IOException {
    URL urlForGetRequest = new URL("https://jsonplaceholder.typicode.com/posts/1");
    String readLine = null;
    HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();
    conection.setRequestMethod("GET");
    conection.setRequestProperty("userId", "a1bcdef"); // set userId its a sample here
    int responseCode = conection.getResponseCode();


    if (responseCode == HttpURLConnection.HTTP_OK) {
        BufferedReader in = new BufferedReader(
            new InputStreamReader(conection.getInputStream()));
        StringBuffer response = new StringBuffer();
        while ((readLine = in .readLine()) != null) {
            response.append(readLine);
        } in .close();
        // print result
        System.out.println("JSON String Result " + response.toString());
        //GetAndPost.POSTRequest(response.toString());
    } else {
        System.out.println("GET NOT WORKED");
    }

}
Comment

PREVIOUS NEXT
Code Example
Java :: open bottomsheet from adapter java 
Java :: No data type for node: org.hibernate.hql.internal.ast.tree.IdentNode -[IDENT] IdentNode: 
Java :: Goodbye to "Inspect Element" 
Java :: how to explicitly declare an array java 
Java :: spring core xml configuration for collection using constructor 
Java :: hdfs get size of directory java 
Java :: RNGestureHandlerButtonViewManager.java uses or overrides a deprecated API 
Java :: logger output to console twice java 
Java :: witch dependency is needed for "ArraysUtils" for the Import? 
Java :: Save SQLite returned data to an object list 
Java :: java multiple implements 
Java :: picking a random string from string array java 
Java :: java how to make a 2d eclipse 
Java :: compile time exception in java 
Java :: Spring AOP 
Java :: fab icon color 
Java :: java 001 
Java :: single line comment in java 
Java :: They say that they have to create an instance to their application. What does it mean? 
Java :: find the length of jtextfeild in java 
Java :: Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader 
Java :: how to add classpath in spring boot 
Java :: setting up javafx in eclipse 
Java :: java bufferedreader 
Java :: rotate matrix in java 
Java :: java tutorialspoint 
Java :: Linked list implementation in Java source code 
Java :: explain java coding standards for classes 
Java :: java timer schedule every day 
Java :: java dato numero big starkoverflow 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =