What are the ways to send HTTPS requests from the desktop java application? An example of the source code is especially interesting.
Answer 1, Authority 100%
The most primitive way is to use URLCONNECTION
. Here is an example without handling errors, encodings and other husks:
Public Static Void Main (String [] Args) Throws Exception {
UrlConnection Connection = New URL ("https://www.dev.java.net/servlets/projectlist") .OpenConnection ();
Inputstream is = connection.getInputStream ();
InputStReamReader Reader = New InputStreamReader (IS);
char [] buffer = new char [256];
INT RC;
STRINGBUILDER SB = new stringbuilder ();
While ((RC = Reader.Read (Buffer))! = -1)
SB.APPEND (Buffer, 0, Rc);
Reader.Close ();
System.Out.printLN (SB);
}
In the event that it turns out that the standard urlConnection
(more precisely, on the fact it is created HttpsurlConnection
) something can not, then use Apache HTTP Client .
Answer 2, Authority 8%
import java.net. *;
Import java.io. *;
Public Class URLReader {
Public Static Void Main (String [] Args) Thrown Exception {
URL Oracle = New URL ("http://www.oracle.com/");
BufferedReader in = new buffereder (
new inputstreamReader (oracle.openstream ()));
String InputLine;
STRINGBUILDER SB = new stringbuilder ();
While ((Inputline = in.ReadLine ())! = NULL)
SB.APPEND (Inputline);
in.close ();
System.Out.printLN (SB);
}
}
source https://docs.orcle.com/javase/tutorial /Networking/urls/readingURL.html
Answer 3, Authority 8%
You can use http-request Built on Apache HTTP API.
string uri = "https://www.dev.java.net/servlets/projectlist";
Httprequest & lt; String & GT; httprequest = httprequestbuilder.createget (URI, String.class)
.responsedeserializer (responsedeseerializer.ignorabledeserializer ()). Build ();
Public Void Send () {
ResponseHandler & lt; String & GT; Response = httprequest.execute (someparamsyouwant);
System.out.PrintLN (response.getstatuscode ());
System.out.PrintLN (response.get ()); // Retuns Response Body
}