Another method for URL Authorization is to use Java 1.1 to access a password-protected URL.

Source: Internet
Author: User
Tags password protection

Http://blog.csdn.net/jserkang/archive/2005/03/13/318623.aspx

 

Connect to 9588 SMS gateway (http protocol), and 9588 of the text message service URLs on iis require authentication,

URL used in the program: http: // UserName: Password@000.000.000.000/SmbpHttpAgent/

Access java code:

URL url = new URL (buf. toString (); System. out. println ("*****" + buf. toString ());
HttpURLConnection conn = (HttpURLConnection) url. openConnection ();
String password = "user: password;
BASE64Encoder base = new BASE64Encoder ();
String encodedPassword = base. encode (password. getBytes ());
Conn. setRequestProperty ("Authorization", "Basic" + encodedPassword );
Conn. setRequestMethod ("GET ");
BufferedReader in = new BufferedReader (new InputStreamReader (conn. getInputStream ()));
While (in. readLine ()! = Null) {out. println (in. readLine (); bool = true ;}

Access asp code

Function sendmessage (mobile, strmessage)
Dim STR, URL, postmsg
Url = "http: // 000.000.000.00/smbphttpagent/sxxxxxxx
 
Set objhttp = server. Createobject ("msxml2.xmlhttp ")
Set xmldoc = server. Createobject ("MSXML. domdocument ")

'Set parameters and their values
Strrequest = url1
Objhttp. Open "Post", postmsg, false, "access account", "access password"
'Setting this Content-Type is very important.
Objhttp. setRequestHeader "Content-Type", "application/X-WWW-form-urlencoded"
Objhttp. Send (strrequest)
Bok = xmldoc. Load (objhttp. responsexml)
'Look at the status value
If objhttp. Status = 200 then
Xmlstr = xmldoc. xml
Response. Write xmlstr
Else
Response. Write objhttp. Status & "<br>"
Response. Write objhttp. statustext
End if
End function

John zukoski

 

Summary
In the tip in the previous article, Java Tip 46 describes how to use a Java program to access a password-protected URL through the new Java 1.2 Authenticator class. What if you or your users go to Java 1.1? Basically, without Java 1.2, you will lose the benefits of Authenticator. But don't be disappointed! This article will show how to access Java 1.1 applets and applications. For R access that requires entering the user name and password, you need to enter the user name and password URLs. (1250 words)

Thanks very much to David Wallace Croft. He tracked and Fixed a bug in the original version of the Base64Converter class ,? The source code of the modified class is listed in this short article.
In the previous article, we introduced the following scenario: you use your favorite browser to surf the Internet and encounter a URL that requires proxy or HTTP server authorization. A standard dialog box appears on the screen. You must enter your user name and password to access the site. For clarity, the on-screen dialog box is shown in.

 

In Java, you will find a problem: when you try to read URL-related information from InputStream, A FileNotFoundException will be thrown.

From Java 1.2, you can use the Authenticator class. As mentioned above, you have learned in Java Tip 46. However, in versions earlier than Java 1.2
Before trying to read data from a URL, you must first know whether the accessed URL is password-protected. If the URL is password-protected, you must manually confirm that
And then access the required content. The password protection response appears in the form of authorization required attributes. This type of attribute uses URLConnection and? Br/> the URL is associated. Once the authorization requirements are met, the above information can be accessed when a URL is reached.

Authorization characters are in the following format: "Authorization: Basic user name: Password". The Basic Authorization field is base64-encoded. (The description of the encoding format is acceptable.
It can be obtained in RFC 1113. An RFC is a "requst for comment" document that describes some Internet standards. In this example, #1113 describes
For information about message encryption and authorization, see the following resources), you can use the BASE64Encoder class in the sun. misc package for encoding, or create your own class.
?

To establish this access, you only need to confirm whether the URL's URLConnection has the "Authorization" attribute. The following steps describe how to proceed.

Step 1: Create a URL

URL url = new URL (urlString );

Step 2: Obtain the user name and password for specificURL

Step 3: Put them in String and separate them with the colon ":"

String userPassword = theUsername + ":" + thePassword;

Step 4: encode the string

String encoding = new sun. misc. BASE64Encoder (). encode (userPassword. getBytes ());

Step 5: Create a URLConnection using a URL

URLConnection uc = url. openConnection ();

Step 6: Set "Authorization" required attribute for URLConnection

Uc. setRequestProperty ("Authorization", "Basic" + encoding );

 

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/jserkang/archive/2005/03/13/318623.aspx

 

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.