Java net HTTPS get 302 jump post address

Source: Internet
Author: User
Tags readline stringbuffer
Package com.url.test;


Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.security.cert.X509Certificate;


Import javax.net.ssl.HttpsURLConnection;
Import Javax.net.ssl.SSLContext;
Import Javax.net.ssl.TrustManager;
Import Javax.net.ssl.X509TrustManager;


Import Org.apache.commons.lang3.StringUtils;


public class Urlprase {


Get HTTP link 302 after a jump address
public static string gethttp (string url) throws IOException {
URL realurl = new URL (URL);
Opening and linking to URLs
HttpURLConnection conn = (httpurlconnection) realurl.openconnection ();
To set common request properties
Conn.setrequestmethod ("get");
Conn.addrequestproperty ("Accept-charset", "UTF-8;");
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setinstancefollowredirects (FALSE);
Establish the actual connection
Conn.connect ();
String location = Conn.getheaderfield ("location");
if (! Stringutils.isblank (location)) {
return gethttp (location);
} else {
return URL;
}
}
Get Http,https link 302 after the jump address
public static string get (string url) throws ioexception{
URL realurl = new URL (URL);
Trustmanager[] TM = {new X509trustmanager () {
Public java.security.cert.x509certificate[] Getacceptedissuers () {
return new java.security.cert.x509certificate[] {};
}


public void checkclienttrusted (x509certificate[] chain, String authtype) {


}


public void checkservertrusted (x509certificate[] chain, String authtype) {


}
} };
try {
Sslcontext sc = sslcontext.getinstance ("TLS");
Sc.init (NULL, TM, New Java.security.SecureRandom ());
Httpsurlconnection.setdefaultsslsocketfactory (Sc.getsocketfactory ());
catch (Exception e) {
E.printstacktrace ();
}


HttpURLConnection conn = (httpurlconnection) realurl.openconnection ();


To set common request properties
Conn.setrequestmethod ("get");
Conn.addrequestproperty ("Accept-charset", "UTF-8;");


Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Conn.setinstancefollowredirects (FALSE);
Conn.setconnecttimeout (3000);
Conn.setreadtimeout (5000);
Establish the actual connection
Conn.connect ();


/*
InputStream InputStream = Conn.getinputstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream, "utf-8"));
String readLine = Reader.readline ();
StringBuffer sb = new StringBuffer ();
while (readLine!= null) {
Sb.append (ReadLine);
ReadLine = Reader.readline ();
}
System.out.println (Sb.tostring ());
if (reader!=null) {
Reader.close ();
}*/
String location = Conn.getheaderfield ("location");
if (! Stringutils.isblank (location)) {
return get (location);
} else {
return URL;
}
}


public static void Main (string[] args) {
String url = "https://e.cn.miaozhen.com/r/k=2036534&p=74ZOF&dx=__IPDX__&rt=2&ns=__IP__&ni=__ iesid__&v=__loc__&xa=__adplatform__&o=https://as.kejet.net/ rejcrtdgouzbq0y2ouexnefdnzlbntc2mdgym0q0?u/qke2nju1mtjfqziymthb/o/rejcrtdgouzbq0y2ouex/m/nefdnzlbntc2mdgym0q0/ Q/afaclick?https://topic.m.autohome.com.cn/marketing/2016/12/mggs/";
String url = "Http://trk.maxuscloud.com/c?ev=type:ac ' Acid=pilot ' On=cpid:cc80027;cid:;mpid:campaign001_display_ 01;adfmt:textlink ' at=aid:autohome03;mdm:display;src:;p ubid:autohome;mdmdt:pc ' go=http%3a%2f% 2fsales.saicmaxus.com%2fp%2fpages%2forderv8003.html ";
String url = "Https://kyfw.12306.cn/otn/login/init";
try {
String FinalURL = gethttp (URL);
String FinalURL = Get (URL);
System.out.println (FinalURL);
catch (Exception e) {
E.printstacktrace ();
}
}
}
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.