Advertisement
Guest User

Untitled

a guest
Apr 12th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. String url = "http://xxxxxx.net:1900/login";
  2. URL obj = new URL(url);
  3. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  4. con.setReadTimeout(5000);
  5. //add reuqest header
  6. con.setRequestMethod("POST");
  7. con.setRequestProperty("User-Agent", USER_AGENT);
  8. con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  9.  
  10. String urlParameters = "username=myusername&password=mypsw";
  11.  
  12. // Send post request
  13. con.setDoOutput(true);
  14. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  15. wr.writeBytes(urlParameters);
  16. wr.flush();
  17. wr.close();
  18.  
  19. int responseCode = con.getResponseCode();
  20. String headerName=null;
  21. String cookie="";
  22. for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
  23. if (headerName.equals("Set-Cookie")) {
  24. cookie = con.getHeaderField(i);
  25. }
  26. }
  27. //print my cookie
  28. System.out.println("PRIMO COOKIE "+ cookie);
  29.  
  30. String url = "http://xxxx.net:1910/myrequest";
  31. URL obj = new URL(url);
  32. HttpURLConnection con = (HttpURLConnection) obj.openConnection();
  33. con.setReadTimeout(5000);
  34. //add reuqest header
  35. con.setRequestProperty("Host", "http://xxxx.net:1910");
  36. con.setRequestProperty("User-Agent", USER_AGENT);
  37. con.setRequestProperty("Accept","*/*");
  38. con.setRequestProperty("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4");
  39. con.setRequestProperty("Cookie", c); //c is String of my cookie
  40. con.setRequestProperty("Connection", "keep-alive");
  41.  
  42. int responseCode = con.getResponseCode();
  43. String headerName=null;
  44.  
  45.  
  46. String cookie="";
  47. for (int i=1; (headerName = con.getHeaderFieldKey(i))!=null; i++) {
  48. if (headerName.equals("Set-Cookie")) {
  49. cookie = con.getHeaderField(i); // <--- the cookie is empty. why?
  50. }
  51. }
  52. System.out.println("SECONDO COOKIE "+ cookie);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement