Java htttp login Return cookie access to other resources through cookies

Source: Internet
Author: User
Tags getmessage set set


Import java.io.*;
Import java.net.HttpURLConnection;
Import java.net.MalformedURLException;
Import Java.net.URL;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;

/**
* Created by Yaoyao on 16/3/19.
*/
public class Test0319 {


int counturl=0;


public static void Main (string[] args) {

try {
String cookie = new test0319 (). Login ();
New test0319 (). getonehtml ("Http://192.168.1.103:8090/front/sku/list", "Utf-8", "[Rememberme=deleteme; Path=/front; max-age=0; Expires=fri, 18-mar-2016 16:15:23 GMT, Jsessionid=68683d96f61b34c1cdfb5431aba5b4ba; path=/front/; HttpOnly] ");
} catch (IOException e) {
E.printstacktrace ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}




public string getonehtml (string htmlurl,string encoding,string cookie) throws IOException, Interruptedexception
{

Up to 5 repeated requests to reverse the crawl
if (counturl==5) {
counturl=0;
return "0";
}
System.out.println (cookie);
StringBuilder content = new StringBuilder ();
HttpURLConnection httpconn = null;
Try
{
URL url = new URL (htmlurl);

Httpconn = (httpurlconnection) url.openconnection ();
Header setting, Get method

Httpurlconnection.setfollowredirects (TRUE);
Httpconn.setrequestmethod ("GET");
Httpconn.setrequestproperty ("User-agent", "mozilla/5.0" (X11; Linux x86_64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1750.152 safari/537.36 ");
Httpconn.setrequestproperty ("Connection", "keep-alive");
Httpconn.setrequestproperty ("Accept", "Text/html,application/xhtml+xml,application/xml");
Httpconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Httpconn.setrequestproperty ("Cookie", cookie);
Httpconn.setrequestproperty ("Cache-control", "No-cache, No-store");
Httpconn.setrequestproperty ("Host", "http://192.168.1.103:8090");
Httpconn.setconnecttimeout (20000);
Httpconn.setreadtimeout (20000);
Logger.info (Httpconn.getresponsemessage ());
BufferedReader in = new BufferedReader (New InputStreamReader (Httpconn.getinputstream (), encoding)),////Open connection, get content
String temp;
while (temp = in.readline ()) = null)
Replace point some useless to symbol
{
Content.append (temp);
}
In.close ();
Httpconn.disconnect ();

}
catch (final malformedurlexception me)
{
System.out.println ("URL does not exist!");
Me.getmessage ();
throw me;
}
catch (final filenotfoundexception me)
{
System.out.println (htmlurl+ "anti-crawl Start");
return "0";
}
catch (Final IOException e)
{
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Anti-crawl start:" +htmlurl+ "number of times:" +counturl++);
Httpconn.disconnect ();
Thread.Sleep (20000);
Return this.getonehtml (Htmlurl, Encoding,cookie);
}

System.out.println (SB);
counturl=0;
Httpconn.disconnect ();

System.out.println (Content.tostring ());
return content.tostring ();

}

Public String Login () throws Malformedurlexception, Interruptedexception {

String Htmlurl = "Http://192.168.1.103:8090/front/login";
HttpURLConnection httpconn = null;
String cookie = "";
try {
URL url = new URL (htmlurl);

Httpconn = (httpurlconnection) url.openconnection ();

Httpurlconnection.setfollowredirects (TRUE);
Httpconn.setrequestmethod ("POST");
Httpconn.setrequestproperty ("User-agent", "mozilla/5.0" (X11; Linux x86_64) applewebkit/537.36 (khtml, like Gecko) chrome/33.0.1750.152 safari/537.36 ");
Httpconn.setrequestproperty ("Connection", "keep-alive");
Httpconn.setrequestproperty ("Accept", "Text/html,application/xhtml+xml,application/xml");
Httpconn.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
Httpconn.setrequestproperty ("Cache-control", "No-cache, No-store");
Httpconn.setrequestproperty ("Host", "Http://192.168.1.103:8090/front");
Httpconn.setrequestproperty ("Referer", "https://www.linkedin.com/uas/login?session_redirect=http:// Www.linkedin.com/profile/view?id=222323610&authType=name&authToken=fcEe ");
Post method, redirection settings
Httpconn.setdooutput (TRUE);
Httpconn.setdoinput (TRUE);
Httpconn.setusecaches (FALSE);
Httpconn.setinstancefollowredirects (FALSE);
Write, the Post method must transmit data in a stream-write manner
StringBuffer str_buf = new StringBuffer (4096);
OutputStream OS = Httpconn.getoutputstream ();
Str_buf.append ("username"). Append ("="). Append ("13062779665"). Append ("&");
Str_buf.append ("Password"). Append ("="). Append ("111111"). Append ("&");
Str_buf.append ("Session_redirect"). Append ("="). Append (Redicturl);
Os.write (Str_buf.tostring (). GetBytes ());
Os.flush ();
Os.close ();
Httpconn.setconnecttimeout (20000);
Httpconn.setreadtimeout (20000);
Get redirects and cookies
String redicturl= Httpconn.getheaderfield ("location");
SYSTEM.OUT.PRINTLN ("First request redirect Address location=" +redicturl);

Get cookies
map<string, list<string>> map = Httpconn.getheaderfields ();
System.out.println (Map.tostring ());
Set set = Map.keyset ();
for (Iterator Iterator = Set.iterator (); Iterator.hasnext ();) {
String key = String.valueof (Iterator.next ());
if (key! = null) {
if (Key.equals ("Set-cookie")) {
System.out.println ("key=" + key + ", start to get Cookie" +map.get ("Set-cookie"). toString ());
list<string> list = Map.get (key);
for (String str:list) {
String temp = str.split ("=") [0];
SYSTEM.OUT.PRINTLN (temp);

The cookie contains very much information and the debug discovery login only needs this message
if (Temp.equals ("Li_at")) {
cookie = str;
return cookie;
//                            }

}
}
}

}
Httpconn.disconnect ();

} catch (Final malformedurlexception me) {
System.out.println ("URL does not exist!");
Me.getmessage ();
throw me;
} catch (Final FileNotFoundException me) {
System.out.println (Htmlurl + "anti-crawl Start");
return "0";
} catch (Final IOException e) {
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Anti-crawl start:" + Htmlurl + "number of times:" + counturl++);
Httpconn.disconnect ();
Thread.Sleep (20000);
return login ();
}
System.out.println (cookie);
return cookie;
}}

Java htttp login Return cookie access to other resources through cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.