Using (HTTP) urlconnection. setrequestproperty

Source: Internet
Author: User

Import java.net. *; <br/> Import Java. io. *; <br/> import sun. misc. *; <br/> public class geturl {<br/> Public static void main (string URL) {<br/> system. out. println (geturl (URL, "", ""); <br/>}< br/> Public static string encode (string source) {<br/> base64encoder ENC = new sun. misc. base64encoder (); <br/> return (ENC. encode (source. getbytes (); <br/>}< br/> Public static string geturl (string URL, String name, string password) <br/>{< br/> string thisline; <br/> string retval; <br/> url u; <br/> urlconnection UC; <br/> retval = ""; <br/> try {u = new URL (URL); <br/> try {<br/> UC = u. openconnection (); <br/> If (name! = NULL) {<br/> UC. setrequestproperty ("Authorization", "Basic" + encode (name + ":" <br/> + password )); <br/>}< br/> inputstream content = (inputstream) UC. getinputstream (); <br/> bufferedreader in = new bufferedreader (New inputstreamreader <br/> (content); <br/> string line; <br/> while (line = in. readline ())! = NULL) {<br/> retval + = line; <br/>}< br/>} catch (exception e) {<br/> return ""; <br/>}< br/>} catch (malformedurlexception e) {<br/> return (URL + "is not a parseable URL "); <br/>}< br/> return retval; <br/>}< br/>}Package HTTP. URL; <br/> Import java.net. *; <br/> Import Java. text. simpledateformat; <br/> Import Java. io. *; <br/> Import javax. XML. parsers. documentbuilder; <br/> Import javax. XML. parsers. documentbuilderfactory; <br/> Import Org. w3C. dom. document; <br/> Import Org. w3C. dom. nodelist; <br/> import sun. misc. *; <br/> public class geturl {<br/> Public static void main (string [] ARGs) {<br/>/** simpledateformat SDF = New simpledateformat ("yyyymmddhhmmss"); <br/> string daytime = SDF. format (New Java. util. date (); </P> <p> writexml (geturl ("http: // 61.177.183.174/cgi-bin/cgi? Required no = 7 & subtype = 0 & sltevent = 15 & channel = 15 & bdate = 1255968000 & edate = 1256054399 ", <br/>" admin "," 08197529 "), daytime); */</P> <p >}< br/> Public static string encode (string source) {<br/> base64encoder ENC = new sun. misc. base64encoder (); <br/> return (ENC. encode (source. getbytes (); <br/>}< br/> Public static string geturl (string URL, string name, string password) {<br/> string thisline; <br/> string RET Val; <br/> url u; <br/> urlconnection UC; <br/> retval = ""; <br/> try {<br/> U = new URL (URL); <br/> try {<br/> UC = u. openconnection (); <br/> If (name! = NULL) {<br/> UC. setrequestproperty ("Authorization", "Basic" + encode (name + ":" + password); <br/>}< br/> inputstream content = (inputstream) UC. getinputstream (); <br/> bufferedreader in = new bufferedreader (New inputstreamreader (content); <br/> string line; <br/> while (line = in. readline ())! = NULL) {<br/> retval + = line; <br/>}< br/>} catch (exception e) {<br/> return "errors! "; <Br/>}< br/>} catch (malformedurlexception e) {<br/> return (URL +" is not a parseable URL "); <br/>}< br/> return retval; <br/>}</P> <p> Public static void writexml (string MSG, string daytime) {<br/> string myrealhtmlfiledir; <br/> myrealhtmlfiledir = "D:/video playback XML"; <br/> If (! New file (myrealhtmlfiledir ). isdirectory () {<br/> New file (myrealhtmlfiledir ). mkdirs (); <br/>}</P> <p> filewriter; <br/> try {<br/> filewriter = new filewriter (myrealhtmlfiledir + "/cgi" + daytime + ". XML ", true); <br/> bufferedwriter mbufwriter = new bufferedwriter (filewriter); <br/> mbufwriter. write (MSG); <br/> mbufwriter. newline (); <br/> mbufwriter. flush (); <br/> mbufwriter. close (); <br/>}catch (ioexception E1) {<br/> e1.printstacktrace (); <br/>}</P> <p >}< br/>}
 

 

Simulate logon in Java and obtain the page information after logon.

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.