How to Do a Http Get in Java

HTTP GET request in java

You can create the URL object using your URL, it will figure out ports and other things itself. Ref : https://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL oracle = new URL("http://localhost:4567/XXXX");
URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}

How to use HTTP GET Requests in Java?

First, read this tutorial on the java HTTP client. (Note that it requires jdk11 or up).

From there it should be fairly simply; that .format() thing is just replacing the {} with the provided ip and query parts. The auth part is more interesting. The verify part presumably means 'whatever, forget about SSL'.

Between a password of 'admin' and 'disregard SSL issues', this code screams "You are about 2 weeks away from getting your box p0wned", maybe you should be taking security a bit more seriously than this.

At any rate, the equivalents in the java sphere are more complicated, because java intentionally does not meant 'disable ssl' to be a casual throwaway move, unlike python which just hands you the bazooka no questions asked.

Here is a tutorial on how to do basic http auth with the http client.

To shoot your foot off properly and ensure that the foot is fully dead, you need to make an SSL Context that does nothing and silently just accepts all certificates, even ones someone trying to hack your system made. Then pass that for .sslContext to HttpClient.builder().

Here is an example of someone firing this bazooka.

how to send http get requests in java and take a specific field

Suppose that you're not limited regarding 3rd party library usage, the following is a very easy example of what you want to achieve.

To do so, it uses both Apache's HTTPClient to perform the GET request and Jackson to deserialize the response.

First you start with creating a model class that representes your expected response object:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Response {

private Integer id;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }

}

Note that the class is annotated with @JsonIgnoreProperties(ignoreUnknown = true) which instructs Jackson to ignore any properties that cannot be mapped to the model class (i.e in our case everything other than the id field).

With this in place performing a GET request and retrieving the response's id field can be done as easy as the following example:

public class HttpClientExample {

public static void main(String... args) {

try (var client = HttpClients.createDefault()) {
var getRequest = new HttpGet("https://jsonplaceholder.typicode.com/todos/1");
getRequest.addHeader("accept", "application/json");

HttpResponse response = client.execute(getRequest);
if (isNot2xx(response.getStatusLine().getStatusCode())) {
throw new IllegalArgumentException("Failed to get with code: " + response.getStatusLine().getStatusCode());
}

Response resp = new ObjectMapper().readValue(EntityUtils.toString(response.getEntity()), Response.class);
System.out.println(resp.getId());

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

}

private static boolean isNot2xx(int statusCode) {
return statusCode != 200;
}

}

As mentioned above, this example assumes that you can use 3rd party libraries. Also note that if you use Java 11 you can omit using the HTTP client from Apache as the new JDK comes bundled with Java's own HTTP client which offers all the functionality you need to perform your job.

How to send HTTP request in java?

You can use java.net.HttpUrlConnection.

Example (from here), with improvements. Included in case of link rot:

public static String executePost(String targetURL, String urlParameters) {
HttpURLConnection connection = null;

try {
//Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");

connection.setRequestProperty("Content-Length",
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");

connection.setUseCaches(false);
connection.setDoOutput(true);

//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.close();

//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
String line;
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

Send content body with HTTP GET Request in Java

You need to use the below

public class HttpGetWithBody extends HttpEntityEnclosingRequestBase {

@Override
public String getMethod() {
return "GET";
}
}

HttpGetWithBody getWithBody = new HttpGetWithBody ();
getWithBody.setEntity(y(new ByteArrayEntity(
"<SOMEPAYLOAD FOR A GET ???>".toString().getBytes("UTF8"))););
getResponse = httpclient.execute(getWithBody );

Import needed will be org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

How to get specific information from a website using JAVA HTTP request

there isn't API at 'investing.com' but simple widget. you should crawl the data OR using API via another site.

i think the Exchange rate information is provided by not only investing.com but also several finance service. And you can using Class URL, HttpUrlConnection in java.net to request for data to target url you need.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URL.html

Having troubles creating an HTTP GET request via Java

Have you ever considered using Apache HttpClient? Code Snippet Attached below seems to work on your mentioned endpoint. Hope this helps.

P.S: You'll have to add this library as a Maven dependency in your pom.xml. Link here.

import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

public class API {
public static void main(String[] args) throws IOException, URISyntaxException {
HttpGet request = new HttpGet("http://mirror.rackspace.com/archlinux/iso/2019.12.01");
URI uri = new URIBuilder(request.getURI())
.setParameter("info_hash", "foo")
.setParameter("peer_id", "bar")
.build();
request.setURI(uri);
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = httpClient.execute(request);
InputStream input = response.getEntity().getContent();
String content = IOUtils.toString(input);
System.out.println(content);
}
}

Response:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of /archlinux/iso/2019.12.01</title>
</head>
<body>
<h1>Index of /archlinux/iso/2019.12.01</h1>
<ul><li><a href="/archlinux/iso/"> Parent Directory</a></li>
<li><a href="arch/"> arch/</a></li>
<li><a href="archlinux-2019.12.01-x86_64.iso"> archlinux-2019.12.01-x86_64.iso</a></li>
<li><a href="archlinux-2019.12.01-x86_64.iso.sig"> archlinux-2019.12.01-x86_64.iso.sig</a></li>
<li><a href="archlinux-2019.12.01-x86_64.iso.torrent"> archlinux-2019.12.01-x86_64.iso.torrent</a></li>
<li><a href="archlinux-bootstrap-2019.12.01-x86_64.tar.gz"> archlinux-bootstrap-2019.12.01-x86_64.tar.gz</a></li>
<li><a href="archlinux-bootstrap-2019.12.01-x86_64.tar.gz.sig"> archlinux-bootstrap-2019.12.01-x86_64.tar.gz.sig</a></li>
<li><a href="md5sums.txt"> md5sums.txt</a></li>
<li><a href="sha1sums.txt"> sha1sums.txt</a></li>
</ul>
Rackers - More on this mirror here: https://rax.io/mirrorfaq
</body></html>


Related Topics



Leave a reply



Submit