[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.0

URLConnection with Proxy & Domain

Asked by jbrizal in Java Programming Language

Tags: urlconnection, proxy

Hai ..  

I try to use this function but my pc not join to any domain and using proxy server to connect to internet ..

i try this .. function

the problem is .. i need pass DOMAIN name to connect to internet ...

please give me advice .. thank .


// The Authenticator class ******************************
import java.io.*;
import java.net.*;

public class URLAuthenticator {
 /**
  * The alphabet dictionary used for encoding
  */
 public static final char [ ]  alphabet = {
 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',   //  0 to  7
 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',   //  8 to 15
 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',   // 16 to 23
 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',   // 24 to 31
 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',   // 32 to 39
 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',   // 40 to 47
 'w', 'x', 'y', 'z', '0', '1', '2', '3',   // 48 to 55
 '4', '5', '6', '7', '8', '9', '+', '/' }; // 56 to 63

 /**
  * Encode a string
  * @param s the string to encode
  * @return the encoded string
  */
 public static String encode(String s) {
   return encode(s.getBytes());
 }

 /**
  * Encode a byte[] array
  * @param octetString the byte[] buffer to encode
  * @return the encoded string
  */
 public static String encode(byte[] octetString) {
   int  bits24;
   int  bits6;
   char[] out = new char[((octetString.length - 1) / 3 + 1) * 4];
   int outIndex = 0;
   int i = 0;
   while ((i + 3) <= octetString.length) {
     // store the octets
     bits24 = (octetString[i++] & 0xFF ) << 16;
     bits24 |= (octetString[i++] & 0xFF ) << 8;
     bits24 |= (octetString[i++] & 0xFF ) << 0;
     bits6 = (bits24 & 0x00FC0000) >> 18;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x0003F000) >> 12;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x00000FC0) >> 6;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x0000003F);
     out[outIndex++] = alphabet[bits6];
   }
   if (octetString.length - i == 2) {
     // store the octets
     bits24 = (octetString[i] & 0xFF ) << 16;
     bits24 |= (octetString[i + 1] & 0xFF) << 8;
     bits6 = (bits24 & 0x00FC0000) >> 18;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x0003F000) >> 12;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x00000FC0) >> 6;
     out[outIndex++] = alphabet[bits6];
     // padding
     out[outIndex++] = '=';
   } else if (octetString.length - i == 1) {
     // store the octets
     bits24 = (octetString[i] & 0xFF) << 16;
     bits6 = (bits24 & 0x00FC0000) >> 18;
     out[outIndex++] = alphabet[bits6];
     bits6 = (bits24 & 0x0003F000) >> 12;
     out[outIndex++] = alphabet[bits6];
     // padding
     out[outIndex++] = '=';
     out[outIndex++] = '=';
   }
   return new String(out);
 }

 
public static void authenticate(URLConnection uc, String user, String password) {
    String encoding = encode((user + ":" + password).getBytes());
    uc.setRequestProperty("Authorization", "Basic " + encoding);
}

 /**
  * Create an authenticated URLConnection
  * @param u the URL to open the connection from
  * @param user the users name
  * @param password the users password
  * @return the authenticated URLConnection
  */
 public static URLConnection openConnection(URL u, String user, String password) {
   URLConnection result = null;
   try {
     result = u.openConnection();
     if (result != null) {
       authenticate(result, user, password);
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return result;
 }

 /**
  * Get a URL content object through an authenticated URLConnection
  * @param u the URL to get the content from
  * @param user the users name
  * @param password the users password
  * @return the content of the URL
  */
 public static Object getContent(URL u, String user, String password) {
   Object result = null;

   try {
     URLConnection uc = openConnection(u, user, password);
     if (uc != null) {
       result = uc.getContent();
     }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return result;
 }

 /**
  * Get a URL InputStream object through an authenticated URLConnection
  * @param u the URL to get the InputStream object from
  * @param user the users name
  * @param password the users password
  * @return the InputStream object of the URL
  */
 public static InputStream openStream(URL u, String user, String password) {
   InputStream result = null;
   try {
     URLConnection uc = openConnection(u, user, password);
     if (uc != null) {
     result = uc.getInputStream();
   }
   } catch (Exception ex) {
     ex.printStackTrace();
   }
   return result;
 }

 public static void main(String args[])throws Exception {
   URL loader = null;
   String line = "";
   
   System.setProperty("java.protocol.handler.pkgs","com.sun.net.ssl.internal.www.protocol");
   
   System.setProperty("proxyHost", "10.8.0.6");
   System.setProperty("proxyPort", "80");
   
   try {
     loader = new URL("http://www.yahoo.com.sg");
   } catch(MalformedURLException mue) {
     System.out.println("Invalid URL !");
   }
   try {
     URLConnection con = URLAuthenticator.openConnection(loader, user_name, password);
     
   new InputStreamReader(con.getInputStream());
     
     BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
     while ((line = br.readLine()) != null)
       if(line!=null)
       System.out.println(line);
     br.close();
   
   }
   catch(IOException e) {
      e.printStackTrace();
   }
 
 }

}
[+][-]06/04/02 04:56 AM, ID: 7053513Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Java Programming Language
Tags: urlconnection, proxy
Sign Up Now!
Solution Provided By: userquin
Participating Experts: 2
Solution Grade: A
 
 
Related Solutions
 
Loading Advertisement...
 
[+][-]06/27/03 07:53 AM, ID: 8813860Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/05/03 09:21 PM, ID: 8862880Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20100608-EE-VQP-165