Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Qadriyah Malik   on Apr 12 In Java Category.

  
Question Answered By: Kawthar Malik   on Apr 12

<code ----ConnectToUrl.java---->
package httpthroughproxy;

import java.net.URL;
import java.net.HttpURLConnection;
import java.util.Properties;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.Socket.*;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import httpthroughproxy.SimpleAuthenticator;

/**
* based on article from
* www.developer.com/java/other/article.php/1551421
*/


public class ConnectToUrl {
public ConnectToUrl() {
}
public static void main(String[] args) {
ConnectToUrl connectToUrl1 = new ConnectToUrl();
connectToUrl1.start2();
}


public static void start1() {
// OPEN connection  THRU proxy  WITHOUT AUTHENTICATION
Test1 MyConn = new Test1();
String url = "http://www.verisign.com/";
String proxy = "172.0.0.1";
String port = "8080";

URL server = null;
try {
server = new URL(url);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", proxy);
systemProperties.setProperty("http.proxyPort", port);
HttpURLConnection connection = null;
InputStream in = null;
try {
connection = (HttpURLConnection) server.openConnection();
connection.connect();
in = connection.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(readFromInputStream(in));
}

public void start2() {
// OPEN CONNECTION THRU PROXY WITH AUTHENTICATION
String url = "http://www.google.com/";
String proxy = "172.0.0.1";
String port = "8080";
String username = "aaaaaa";
String password = "bbbbbb";
Authenticator.setDefault(new SimpleAuthenticator(username,
password));
URL server = null;
try {
server = new URL(url);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", proxy);
systemProperties.setProperty("http.proxyPort", port);
InputStream in = null;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) server.openConnection();
connection.connect();
in = connection.getInputStream();
}
catch (IOException e) {
e.printStackTrace();
}
System.out.println(readFromInputStream(in));
}

public static String readFromInputStream(InputStream in) {
StringBuffer strBuf = new StringBuffer();
char ac[];
//java.io.InputStream ip = conn.getInputStream();
BufferedReader buf = new BufferedReader(new InputStreamReader
(in));
// tq que buf contient des données (buf.ready())
// crée un nouveau tableau ac de 10000 char maxi
// met le contenu de buf dans ac
// ajoute ac à strBuf
try {
while (buf.ready()) {
ac = new char[10000];
buf.read(ac);
strBuf.append(ac);
}
buf.close();
}
catch (IOException e) {
e.printStackTrace();
}
return strBuf.toString().trim();
}

}
</code ----ConnectToUrl.java---->


<code ----SimpleAuthenticator.java---->
package httpthroughproxy;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class SimpleAuthenticator extends Authenticator
{
private String username;
private String password;

public SimpleAuthenticator(String username,String password)
{
this.username = username;
this.password = password;
}

protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(
username,password.toCharArray());
}
}
</code ----SimpleAuthenticator.java---->

Share: 

 
 
Didn't find what you were looking for? Find more on code example to connect through a proxy (http and https) ? Or get search suggestion and latest updates.


Tagged: