Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Emily Diaz   on Feb 16 In Java Category.

  
Question Answered By: Troy Kelley   on Feb 16

Add the following 2 lines before creating a socket if u r behind some proxy..

System.setProperty("http.proxyHost","192.168.0.85"); //Change to ur Proxy
Address
System.setProperty("http.proxyPort","8080"); // Change port for ur setup

I hv added a small sample code also here..
these 2 properties enables the java  Sockets to be created behind a Proxy /
Firewall..

regards,
S.Vasanth Kumar.

import java.net.*;
import java.io.*;
public class ProxySocket2
{
public static void main(String args[]) throws Exception
{
System.setProperty("http.proxyHost","192.168.0.85");
System.setProperty("http.proxyPort","8080");

URLConnection url = new URL("http://www.xyz.com:80").openConnection();
// Socket s = new Socket("216.109.112.135",80);
InputStream in = url.getInputStream();
BufferedInputStream bIn = new BufferedInputStream(in);
int n=-1;
byte b[] = new byte[5000];
StringBuffer sb = new StringBuffer();
while((n=bIn.read(b,0,b.length))!=-1)
{
for(int i=0;i<n;i++)
{
sb.append((char)b[i]);
}
}
bIn.close();

System.out.println(sb.toString());
}
}

Share: 

 

This Question has 8 more answer(s). View Complete Question Thread

 


Tagged: