Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting to Internet from my Server

 
Venkatesh Waran
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i have created one simple server using Socket to get the URL given in the Browser. When running this Server i got several Browser Request details such as User Agent, Connection status, Host name, Accept-language etc..,
I m storing all those in one String array and taken only the Host name...,
But when i m making this server as my Proxy server in IE settings it works and displays those details but Webpage not get opened...,
How could i open an Website given in t browser from this server....?



here is my code


import java.io.*;
import java.net.*;
import java.awt.Desktop;

public class requests {



public static void main(String[] args) {
String line;
String[] x=new String[100];
int j=0;
Socket so;
StringBuffer req;
ServerSocket serso=null;
int port=8080;
int i=0;


try
{
serso=new ServerSocket(port);
}
catch(Exception e)
{
System.out.println("Error1: "+e);
System.exit(0);
}

while(true)
{
j=0;
while(j<100)
{
x[j]="";
j++;
}

try
{
System.out.println("Listening on Port "+port+"...");
so=serso.accept();
System.out.println("Connection Established...,\n");
BufferedReader in=new BufferedReader(new InputStreamReader(so.getInputStream()));
req=new StringBuffer();
PrintWriter out=new PrintWriter(so.getOutputStream());
while(true)
{
StringBuffer form=new StringBuffer();
line=in.readLine();
x[co++]=line;
System.out.println(line);
req.append(line+"\n");
if(line.equals(""))
{
while(in.ready())
{
char c=(char)in.read();
form.append(c);
}
System.out.println(form.toString());
req.append(form.toString());
break;
}
}
System.out.println("value is "+x[5]);
String ss=x[5];
ss=ss.substring(6,ss.length());
System.out.println("String is "+ss);
URL yahoo = new URL(ss);
/* To Get the Code of the Site
BufferedReader in1 = new BufferedReader(new InputStreamReader(yahoo.openStream()));
StringWriter sw=new StringWriter();
PrintWriter pw=new PrintWriter(sw);
String inputLine;
StringBuffer sbb=new StringBuffer();

while ((inputLine = in1.readLine()) != null)
{
System.out.println(inputLine);
sbb.append(inputLine);
pw.println(inputLine);
}
in1.close();*/
so.close();
}
catch(Exception e)
{
System.err.println("Error2 "+e);
break;
}

}
}
// TODO code application logic here

}




d:\>javac requests.jave
d:\>java requests


when running by giving http://localhost:8080 in the browser you will get output in Dos Prompt as

GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shock
wave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application
/msword, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: localhost:8080
Connection: Keep-Alive






if you give http://www.coderanch.com in browser means you will get output as

GET http://www.coderanch.com/templates/default/images/button.gif HTTP/1.0
Accept: */*
Referer: http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum
_id=8
Accept-Language: en-us
Proxy-Connection: Keep-Alive
If-Modified-Since: Sat, 02 Feb 2008 23:27:50 GMT
If-None-Match: W/"295-1201994870000"
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)
Host: www.coderanch.com
Cookie: __utma=135885091.87697471.1236260726.1236262559.1236263014.3; __utmz=135
885091.1236260727.1.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=conne
ct%20to%20internet%20from%20socket%20server%20using%20java; __utmv=135885091.hou
r%3A%2019; __utmb=135885091.35.5.1236263230187; __utmc=135885091; JSESSIONID=C9E
58BB5DA4C7C6A210B8473B0F80D2C



please anyone help me....
Thanks...,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic