logo
down
shadow

How to convert a cURL call into a Java URLConnection call


How to convert a cURL call into a Java URLConnection call

By : Patrick Howell ONeil
Date : November 23 2020, 03:01 PM
wish helps you There are multiple ways to do it. Firstly, since you want to send a JSON object, you might want to use a JSON library, for example, Google's gson. But to make it easy you can just send the request as a String. Here is a sample code that sends your JSON to your URL.
code :
HttpClient httpClient = HttpClientBuilder.create().build(); 

try {

    HttpPost request = new HttpPost("https://localhost:9999/api/traces/%2f/login");
    StringEntity params =new StringEntity("{\"mobile_number\":\"09178005343\", \"pin\":\"1111\"");
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);

    //Do what you want with the response

}catch (Exception ex) {

    //If exception occurs handle it

} finally {
     //Close the connection 
}


Share : facebook icon twitter icon
convert curl call into java urlconnection call

convert curl call into java urlconnection call


By : Nick Norris
Date : March 29 2020, 07:55 AM
will help you I have curl command: , This is final solution:
code :
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.Proxy;
import java.net.InetSocketAddress;
import java.io.OutputStreamWriter;

public class Curl {

  public static void main(String[] args) {

    try {

    String url = "http://127.0.0.1:15672/api/traces/%2f/trololo";

    URL obj = new URL(url);
    HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

    conn.setRequestProperty("Content-Type", "application/json");
    conn.setDoOutput(true);

    conn.setRequestMethod("PUT");

    String userpass = "user" + ":" + "pass";
    String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes("UTF-8"));
    conn.setRequestProperty ("Authorization", basicAuth);

    String data =  "{\"format\":\"json\",\"pattern\":\"#\"}";
    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write(data);
    out.close();

    new InputStreamReader(conn.getInputStream());   

    } catch (Exception e) {
    e.printStackTrace();
    }

  }

}
Convert cURL call to Java

Convert cURL call to Java


By : georgiaGrown
Date : March 29 2020, 07:55 AM
seems to work fine You are using Set-Cookie (that comes from server in response header) whereas it should be only Cookie: (goes from client in request header).
code :
httpGet.addHeader("Cookie","JSESSIONID=ekl23l2-3321-1930-b889-kelwek23b8v9; Path=/; HttpOnly");
Convert cURL to Java for SOAP Call

Convert cURL to Java for SOAP Call


By : tkar
Date : March 29 2020, 07:55 AM
I wish this helpful for you Im implementing a SOAP web service and it is working with a cURL call. I implemented following this tutorial. The service is working with the following command: , Add below lines to your code at the end, it will do the JOB.
code :
OutputStream wr = new DataOutputStream(conn.getOutputStream());


    BufferedReader br = new BufferedReader(new FileReader(new File("request.xml")));

    //reading file and writing to URL
    System.out.println("Request:-");
    String st;
    while ((st = br.readLine()) != null) {
        System.out.print(st);
        wr.write(st.getBytes());
    }

    //Flush&close the writing to URL.
    wr.flush();
    wr.close();

    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String output;

    StringBuffer response = new StringBuffer();
    while ((output = in.readLine()) != null) {
        response.append(output);
    }

    in.close();


    // printing result from response
    System.out.println("Response:-" + response.toString());
How to convert simple Curl call to Java HttpURLConnection

How to convert simple Curl call to Java HttpURLConnection


By : Rinecamo
Date : March 29 2020, 07:55 AM
around this issue The URL you are using in java code is wrong with escape characters used for curl. Remove the escape characters and use the url https://api.github.com/search/repositories?q=reactive:
code :
public static void main(String[] args) throws IOException {
        URL url = new URL("https://api.github.com/search/repositories?q=reactive");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "application/json");
        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);

        System.out.println(con.getResponseMessage());
}
OK
How convert curl call into java HttpPost: curl --form "archive=@test.pdf;type=application/pdf"

How convert curl call into java HttpPost: curl --form "archive=@test.pdf;type=application/pdf"


By : Gamal Mohamed
Date : March 29 2020, 07:55 AM
With these it helps How convert curl call into java HttpPost: , I solved my problem below the solution I used:
shadow
Privacy Policy - Terms - Contact Us © voile276.org