I need to add a few cookies to authorize on website. Cookies are added successfully, but they are missing when making a request:
import java.io.*;
import java.net.*;
public class Main {
static public void main(String[] args) throws Exception {
CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
CookieStore cookieJar = cookieManager.getCookieStore();
CookieHandler.setDefault(cookieManager);
HttpCookie cookie = new HttpCookie("name123", "value123");
cookieJar.add(new URI("http://httpbin.org"), cookie);
HttpURLConnection connection = (HttpURLConnection) new URL("http://httpbin.org/cookies").openConnection();
connection.setRequestMethod("GET");
connection.connect();
BufferedReader in;
StringBuilder response = new StringBuilder();
String inputLine;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
}
}
But I only get an empty map of cookies in response
{ "cookies": {}}
Please, tell me what I should do to fix it.
cookie.setPath( "/" );
cookie.setVersion( 0 );
Do the trick ¯\_(ツ)_/¯
Related
This line conn.setRequestProperty("Cookie",cookieHeader); wont reed cookieHeader if i manualy pass in the string like this conn.setRequestProperty("Cookie", "ci_session=5dsjqh39nkj7nm98nokrukoesr6iir67"); then it works fine, anny idea why this dos not work?
package javaapplication5;
import java.io.*;
import java.net.*;
import java.util.*;
public class JavaApplication5 {
public static void main(String[] args) throws Exception {
String urlString = "http://example.se/";
String params ="";
int timeout =1000;
String cookieHeader = getcookieHeader(urlString,params);
System.out.println(post(urlString,params,cookieHeader,timeout));
}
public static String getcookieHeader(String urlString, String params) throws MalformedURLException, IOException{
// your first request that does the authentication
URL authUrl = new URL(urlString);
HttpURLConnection authCon = (HttpURLConnection) authUrl.openConnection();
authCon.connect();
// temporary to build request cookie header
StringBuilder sb = new StringBuilder();
// find the cookies in the response header from the first request
List<String> cookies = authCon.getHeaderFields().get("Set-Cookie");
if (cookies != null) {
for (String cookie : cookies) {
if (sb.length() > 0) {
sb.append("; ");
}
// only want the first part of the cookie header that has the value
String value = cookie.split(";")[0];
sb.append(value);
}
}
// build request cookie header to send on all subsequent requests
String cookieHeader = sb.toString();
return cookieHeader;
}
public static String post(String urlString, String params, String cookieHeader, int timeout) throws Exception {
String response = null;
HttpURLConnection conn = null;
DataOutputStream dos = null;
InputStream is = null;
try {
URL url = new URL(urlString);
conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(timeout);
conn.setRequestMethod("POST");
conn.setRequestProperty("Cookie", cookieHeader);// does not work
//conn.setRequestProperty("Cookie", "ci_session=5dsjqh39nkj7nm98nokrukoesr6iir67"); works fine..
System.out.println(cookieHeader);// prints out "ci_session=5dsjqh39nkj7nm98nokrukoesr6iir67"
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
conn.connect();
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(params);
dos.flush();
is = conn.getInputStream();
Scanner s = new Scanner(is).useDelimiter("\\A");
response = s.hasNext() ? s.next() : null;
} finally {
if (dos != null) {
dos.close();
}
if (is != null) {
is.close();
}
if (conn != null) {
conn.disconnect();
}
}
return response;
}
}
I found the solution.. Just added this line: CookieHandler.setDefault(new CookieManager()); to getcookieHeader() under authCon.connect(); and removed
conn.setRequestProperty("Cookie", cookieHeader);
With CookieHandler.setDefault(new CookieManager()); HttpURLConnection will automatically save any cookies it receives and send them back with the next request to the same host.
I want to get Json from https://anapioficeandfire.com/api/characters/583 with native Java
Here is the code:
import java.net.*;
import java.io.*;
public class Main
{
public static void main(String[] args) throws Exception {
URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
BufferedReader in = new BufferedReader(
new InputStreamReader(oracle.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
What I get is this error:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://anapioficeandfire.com/api/characters/583
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at sprint2.fireandice.Main.main(Main.java:17)
This code works with example.com, google.com etc...
The problem is with openStream(). The server rejects this Connection and sends 403 Forbidden. You can "fool" the server and act like a normal browser by setting a user-agent.
public static void main(String[] args) throws Exception{
URL oracle = new URL("https://anapioficeandfire.com/api/characters/583");
HttpURLConnection httpcon = (HttpURLConnection) oracle.openConnection();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
I'm developing a small program that performs login via HttpUrlConnection in a session-based screen, save the cookies and go to another page using the saved cookies. My code looks like this:
public class LoginConnection {
private final String USER_AGENT = "Mozilla/5.0";
private CookieStore cookieJar;
public static void main(String[] args) throws Exception {
LoginConnection http = new LoginConnection();
http.sendPost();
http.sendGet();
}
public LoginConnection() {
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);
cookieJar = manager.getCookieStore();
}
private void sendPost() throws Exception {
String params = "login=username&password=passwd";
URL url = new URL("http://www.domain.com/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Accept-Language", "pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4");
// Send post request
connection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(params);
wr.flush();
wr.close();
// send request
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(params);
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line, response = "";
while ((line = in.readLine()) != null) {
response += line + "\n";
}
in.close();
String headerName = null;
for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = connection.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
String cookieName = cookie.substring(0, cookie.indexOf("="));
String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());
HttpCookie httpCookie = new HttpCookie(cookieName, cookieValue);
httpCookie.setDomain("http://www.domain.com/");
httpCookie.setPath("/");
httpCookie.setVersion(0);
cookieJar.add(new URI("http://www.domain.com/"), httpCookie);
}
}
// show cookies (sessionid)
System.out.println("CookieHandler retrieved cookie: " + cookieJar.getCookies().get(0).toString());
// print result
System.out.println(response.toString().replaceAll("\\s+", " "));
}
// HTTP GET request
private void sendGet() throws Exception {
URL url = new URL("http://www.domain.com/profile");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
// add request header
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Cookie", cookieJar.getCookies().get(0).toString());
con.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line, response = "";
while ((line = in.readLine()) != null) {
response += line + "\n";
}
in.close();
// show cookies (sessionid)
System.out.println("CookieHandler retrieved cookie: " + cookieJar.getCookies().get(0).toString());
// print result
System.out.println(response.toString().replaceAll("\\s+", " "));
}
}
The problem is that even sending cookies, the server is returning to the login page. Can someone help me?
I have some controller and I need to send request to another domain and get response result body (it is every time html). How can I do it? Is HttpURLConnection only solution?
#RequestMapping("/")
public ModelAndView mainPage(){
ModelAndView view = new ModelAndView("main");
String conten = /*Here is request result to another domain(result is just html, not JSON)*/
view.addAttribute("statistic","conten);
return view;
}
Here is an exemple to make a request :
String url = "http://www.google.com/";
URL url= new URL(url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
// Important to be thread-safe
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print html string
System.out.println(response.toString());
For a more simpler way
private static String readUrl(String urlString) throws Exception {
BufferedReader reader = null;
try {
URL url = new URL(urlString);
reader = new BufferedReader(
new InputStreamReader(url.openStream()));
StringBuffer buffer = new StringBuffer();
int read;
char[] chars = new char[1024];
while ((read = reader.read(chars)) != -1)
buffer.append(chars, 0, read);
return buffer.toString();
} finally {
if (reader != null)
reader.close();
}
}
Good for reading web-services (JSON Response).
I want to make a java program to auto login at http://www.eclass.teikal.gr/eclass2/
When I run this I take as result the same page! Where am I doing wrong ?
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
// String url = "https://selfsolve.apple.com/wcResults.do";
String url = "http://www.eclass.teikal.gr/eclass2/index.php";
URL obj = new URL(url);
URL obj1 = new URL ("http://www.eclass.teikal.gr/eclass2/");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "uname=my_username&pass=my_password&submit=";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
Try to submit this request from some other tool, for example firefox plugin HTTP Resource test. Then you will find if the problem is in your request or in your code.