hitting a URL using java [duplicate] - java

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
How to send HTTP request in java?
How to use java.net.URLConnection to fire and handle HTTP requests?
using java, how do i hit any url?
for instance, opening of http://www.xyz.com/node1 in a browser will tell xyz.com that node1 is hit.
so in this java program (which sends sms text say 'node1' in example above embedded in the url itself to a sms gateway server)
how do i achieve it without opening any browser or using servlet.

You can use an HttpURLConnection.
But using it directly is overkill if you just want to load the URL in question. This guide shows you how to open a URL.
Basically it boils down to:
URL url = new URL("http://www.xyz.com/node1");
URLConnection conn = url.openConnection();
conn.connect();
//...

The simplest way is to use URL http://download.oracle.com/javase/1.4.2/docs/api/java/net/URL.html. For more advanced/flexible URL fetching you could use HttpClient http://hc.apache.org/httpclient-3.x/

Related

Working with Spotify API - How to follow multiple redirect links in Java?

I'm doing a Hyperskill project which uses Spotify Web Api. I'm using a Spotify Api Wrapper library (https://github.com/thelinmichael/spotify-web-api-java). I'm working with Authorization Code Flow (https://developer.spotify.com/documentation/general/guides/authorization-guide/#authorization-code-flow). This means that when the user grants access, Spotify redirects me to localhost with the access token.
Long story short, I am able to create a localhost server with ServerSocket and I can make the user to grant or deny access to Spotify and it's perfectly generates me the access token in the localhost link.
My problem is that I can't get this access token/code from the callback link (https://example.com/callback?code=NApCCg..BkWtQ&state=profile%2Factivity). I know that I have to follow redirecting, and my guess is that Spotify makes more than 1 redirecting because my code outputs this:
https://accounts.spotify.com/login?continue=https%3A%2F%2Faccounts.spotify.com%2Fauthorize%3Fclient_id%123EXAMPLECODE%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A8080%26response_type%3Dcode
URL url = new URL("https://accounts.spotify.com/authorize?client_id=123EXAMPLECODE&redirect_uri=http://localhost:8080&response_type=code");
HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
ucon.setInstanceFollowRedirects(false);
URL secondURL = new URL(ucon.getHeaderField("Location"));
URLConnection conn = secondURL.openConnection();
System.out.println(secondURL);
You can see that it redirects me from "authorize" to "login" and THEN back to localhost and I am able to follow the redirecting to login. How can I make it following to localhost? Or is there any way to get this code from the callback?
I'd appreciate any help!
I managed to get the token from the callback link with getQuery() method of HttpExchange. Please consider this question as solved.

Java Apache HTTPComponent POST Method

I am designing a third party application that requires a POST request to be sent to a php file on a website and hopefully I should get a response. The site requires me to be logged in in order to make this request normally through the site by pressing a button on it. If I do
Url obj = new URL("http://www.dota2lounge.com/ajax/bumpTrade.php";
HttpUrlConnection con = (HttpUrlConnection) obj.openConnection();
con.setRequestProperty("User-Agent", "Chrome/36.0.1916.144");
And then continue to carry out the POST request, will the site recognize that I am sending this from my Chrome browser in which I am already logged in? Thanks
will the site recognize that I am sending this from my Chrome browser in which I am already logged in?
No, it will not. Imagine how easy it would be to spoof the authentication system of a web application if it worked that way.
Logins typically work by sending Cookies or other headers. You need to send those to authenticate your request. For this to work as if you were logged in with your Chrome application, you'll need to find the corresponding cookies that Chrome stored and send those.
You can find from the link i shared how you can make the authentication.
https://stackoverflow.com/a/3283496/1257445
After you have made an authentication you can make a post request using the session

Getting content from url which requires cookies to be enabled

Using java, how do I get the content from a webpage where cookies and javascript are required to load the page?
Without code or more information it's hard to help.
You'll want a URLConnection with a CookieHandler, you can find some example code here:
https://blogs.oracle.com/CoreJavaTechTips/entry/cookie_handling_in_java_se
You should try learning about HttpURLConnection and how to send information like cookies to websites via Http headers

Spring MVC Controller Action through HttpURLConnection

I wanted to call a SPRING MVC action using HttpURLConnection, just wanted to run this as a background action.....
String logoutUrl = "http://www.mysite.com/logout.sho";
URL url = new URL(logoutUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
etc etc....
The code runs perfectly fine but the action was not performed. But, if I try to perform this action using browser: http://www.mysite.com/logout.sho, it logs out me. Kindly let me know how to tackle this issue or any other way to solve this problem.
If you open a URL connection with a Java program, you're not using the same session ID as the one used by your browser, so obviously, logging out from Java doesn't log you out from the browser.
Imagine if that works, I would just need to write a program which logs out from gmail.com, and everybody gmail user would be logged out.
It's because Spring MVC recognizes your HttpURLConnection as a completely separate, independent session (user). Try opening this URL in a different browser or from a different computer - obviously it won't log you out.
The reason you are not recognized is because you do not provide any session-tracking information like JSESSIONID cookie or rewritten URL. You can pass them from Java, but it's a bit of a hack. It works from your browser simply because every request to mysite.com includes JSESSIONID cookie transparently and Spring MVC maps it to an active session.
That being said, what do you want to achieve?

how to send HTTP request to a servlet [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send HTTP request in java?
I only have one servlet running on the Tomcat server side. Now I want to send a HTTP request to this servlet from a Swing application, and it's not an APPLET application (because I see some examples sending request from applet). How can I do this?
While you can open a direct socket connection and send the raw HTTP headers & content and receive a response back, I would urge you to take a look at HttpRequestBase.

Categories