How to inspect the HTTP request headers sent through a HttpsURLConnection? - java

The simple Java code below works. Is there an easy way to find out / inspect the HTTP request (not the response) headers actually sent?
import java.net.URL;
import java.io.*;
import javax.net.ssl.HttpsURLConnection;
public class Test {
public static void main(String[] args) throws Exception {
String httpsURL = "https://api.gdax.com/products/BTC-USD/book?level=1";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
}
}

I think you can not do it programaticaly, you could use some kind of proxy like TCP/IP monitor in Eclipse. Or enable the debbug option for java adding these options:
-Djava.util.logging.config.file=logging.properties
And put in logging.properties (by default in JRE_HOME\lib) the following property
sun.net.www.protocol.http.HttpsURLConnection.level = ALL

Related

Minecraft auth server returning 403?

So I'm trying to make a custom launcher for my custom Minecraft client but I need a session id to launch the game. This is the code I'm using to try and get a session ID:
package net.arachnamc;
import org.json.JSONObject;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class Main {
public static void main(String[] args) throws IOException {
String AUTH_SERVER = "https://authserver.mojang.com/authenticate";
String json = String.format(
"{" +
"\"clientToken\":\"%s\"," +
"\"username\":\"%s\"," +
"\"password\":\"%s\"" +
"}",
UUID.randomUUID().toString(),
"Koolade446",
"MyPasswordCensored"
);
JSONObject jso = new JSONObject(json);
System.out.println(json);
URL url = new URL(AUTH_SERVER);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
OutputStream os = urlConnection.getOutputStream();
os.write(json.getBytes(StandardCharsets.UTF_8));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
urlConnection.disconnect();
}
}
However, the server returns a 403 forbidden every time. I use a Microsoft account but I can't find any documentation on how to authenticate a Microsoft account so I assumed this was it. Any help is appreciated.

Getting 405 error when making a HTTP get request to a webservice

I'm making the HTTP GET request from a basic java program to a URL which happens to be "http://localhost:9992/users/john.doe#example.com":
public class Tester {
public static void main(String[] args) {
HttpRequester.doesUserExist("john.doe#example.com");
}
}
The implementation of the HTTPRequester is this:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpRequester {
private static HttpURLConnection connection = null;
public static boolean doesUserExist(final String email) {
final String targetUrl = Constants.URL_USER_SRVC + email;
System.out.println(targetUrl);
try {
URL url = new URL(targetUrl);
connection = (HttpURLConnection) url.openConnection();
System.out.println(connection.getRequestMethod());
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setUseCaches(false);
connection.setDoOutput(true);
DataOutputStream outputStream = new DataOutputStream(
connection.getOutputStream());
outputStream.writeBytes(email);
outputStream.close();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer response = new StringBuffer();
String line;
while((line = reader.readLine()) != null) {
response.append(line);
response.append('\r');
}
reader.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
The webservice embedded in a Grizzly server and here are the APIs:
#Path("/users")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class UserResource {
#GET
public List<User> getUsers() {
return UserService.getUsers();
}
#GET
#Path("/{userEmail}")
public User getUser(#PathParam("userEmail") String userEmail) {
return UserService.getUser(userEmail);
}
}
Please take note that the webservice and java program are two separate projects. When I execute the main method I get this error output in the console:
java.io.IOException: Server returned HTTP response code: 405 for URL: http://localhost:9992/users/john.doe#example.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
at com.axa.visualizr.auth.utils.HttpRequester.doesUserExist(HttpRequester.java:30)
at com.axa.visualizr.auth.core.Tester.main(Tester.java:8)
What I think is odd is that the error outputs this at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627) even though I imported java.net.HttpURLConnection;. I have tried opening the declaration on that line but Eclipse states that it is not an valid line number in that class.
I don't know why it's saying that the method is not allowed when I have setRequestMethod("GET"). Maybe it might be because the java program is not running on any kind of server? The Tester.java is only for testing purposes eventually I will move HTTPRequester to another webservice and make the call from webservice A to webservice B.
Since you are passing something in the requestbody, java is interpreting it as a POST request. Please remove below lines from your code and try:
DataOutputStream outputStream = new DataOutputStream(
connection.getOutputStream());
outputStream.writeBytes(email);
outputStream.close();
Try below code:
final String targetUrl = Constants.URL_USER_SRVC + URLEncoder.encode(email, "UTF-8");
instead of: final String targetUrl = Constants.URL_USER_SRVC + email;

Java: Accessing a File from an FTP Server

So I have this FTP server with a bunch of folders and files inside.
My program needs to access this server, read all of the files, and display their data.
For development purposes I've been working with the files on my hard drive, right in the "src" folder.
But now that the server is up and running, I need to connect the software to it.
Basically what I want to do is get a list of the Files in a particular folder on the server.
This is what I have so far:
URL url = null;
File folder = null;
try {
url = new URL ("ftp://username:password#www.superland.example/server");
folder = new File (url.toURI());
} catch (Exception e) {
e.printStackTrace();
}
data = Arrays.asList(folder.listFiles(new FileFilter () {
public boolean accept(File file) {
return file.isDirectory();
}
}));
But I get the error "URI scheme is not 'file'."
I understand this is because my URL starts with "ftp://" and not "file:"
However I can't seem to figure out what I'm supposed to do about it!
Maybe there's a better way to go about this?
File objects cannot handle an FTP connection, you need to use a URLConnection:
URL url = new URL ("ftp://username:password#www.superland.example/server");
URLConnection urlc = url.openConnection();
InputStream is = urlc.getInputStream();
...
Consider as an alternative FTPClient from Apache Commons Net which has support for many protocols. Here is an FTP list files example.
if you use URI with file you can use your code but , but when you want to use ftp so you need to this kind of code; code list the name of the files under your ftp server
import java.net.*;
import java.io.*;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL url = new URL("ftp://username:password#www.superland.example/server");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
EDITED Demo Code Belongs to Codejava
package net.codejava.ftp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class FtpUrlListing {
public static void main(String[] args) {
String ftpUrl = "ftp://%s:%s#%s/%s;type=d";
String host = "www.myserver.com";
String user = "tom";
String pass = "secret";
String dirPath = "/projects/java";
ftpUrl = String.format(ftpUrl, user, pass, host, dirPath);
System.out.println("URL: " + ftpUrl);
try {
URL url = new URL(ftpUrl);
URLConnection conn = url.openConnection();
InputStream inputStream = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
System.out.println("--- START ---");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println("--- END ---");
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

how to get url html contents to string in java

I have a html file stored on the server. I have the URL path something like this: <https://localhost:9443/genesis/Receipt/Receipt.html >
I want to read the contents of this html file which would contain tags, from the url i.e. the source code of the html file.
How am I supposed to do this? This is a server side code and can't have a browser object and I am not sure using a URLConnection would be a good option.
What should be the best solution now?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class URLContent {
public static void main(String[] args) {
try {
// get URL content
String a = "http://localhost:8080//TestWeb/index.jsp";
URL url = new URL(a);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = br.readLine()) != null) {
System.out.println(inputLine);
}
br.close();
System.out.println("Done");
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Resolved it using spring
added the bean to the spring config file
<bean id = "receiptTemplate" class="org.springframework.core.io.ClassPathResource">
<constructor-arg value="/WEB-INF/Receipt/Receipt.html"></constructor-arg>
</bean>
then read it in my method
// read the file into a resource
ClassPathResource fileResource =
(ClassPathResource)context.getApplicationContext().getBean("receiptTemplate");
BufferedReader br = new BufferedReader(new FileReader(fileResource.getFile()));
String line;
StringBuffer sb =
new StringBuffer();
// read contents line by line and store in the string
while ((line =
br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
import java.net.*;
import java.io.*;
//...
URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
url.openConnection();
InputStream reader = url.openStream();
For exemple :
URL url = new URL("https://localhost:9443/genesis/Receipt/Receipt.html");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String l;
while ((l=in.readLine())!=null) {
System.out.println(l);
}
You could use the inputstream in other ways, not just printing it.
Of course if you have the path to the local file, you can also do
InputStream in = new FileInputStream(new File(yourPath));
Simplest way in my opinion is to use IOUtils
import com.amazonaws.util.IOUtils;
...
String uri = "https://localhost:9443/genesis/Receipt/Receipt.html";
String fileContents = IOUtils.toString(new URL(uri).openStream());
System.out.println(fileContents);

How can i made soap Request and Response in Java?

How can I execute the soap webservices and how can I print the data?
Currently I am using the following code
package com.appulento.pack;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class SimpleHTTPRequest
{
public static void main(String[] args) throws Exception {
final String url =
"http://**********:8000/sap/bc/srt/rfc/sap/zmaterials_details/" +
"800/zmaterials_details/zmaterials_details_bind",
soapAction ="urn:sap-com:document:sap:soap:functions:mc-style/ZMATERIALS_DETAILS",
envelope1="<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" +
" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">" +
"<soapenv:Header>"+
"<soapenv:Body>"+
"<urn:ZMATERIALS_DETAILS>"+
"<Language>D</Language>"+
"<MaterialGroup>00208</MaterialGroup>"+
"</urn:ZMATERIALS_DETAILS>"+
"</soap:Body>"+
"</soap:Envelope>" ;
HttpURLConnection connection = null;
try {
final URL serverAddress = new URL("http://*********:8000/sap/bc/srt/wsdl/"+
"srvc_14DAE9C8D79F1EE196F1FC6C6518A345/wsdl11/allinone/ws_policy/" +
"document?sap-client=800&sap-user=************&sap-password=****");
connection = (HttpURLConnection)serverAddress.openConnection();
connection.setRequestProperty("SOAPAction", soapAction);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
final OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.append(envelope1);
writer.close();
final BufferedReader rd =
new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = rd.readLine()) != null) System.out.println(line);
} finally { connection.disconnect(); }
}
}
I want send xml as input request and I want to display in xml too.
Iit's possible to sent HTTP request using httpConnection and parse response, like you do.
But it is already written by other people, use wsimport tool with -keep option. It will generate for you Java artifacts for sending request using SOAP.

Categories