Proxy in Java Question

imported_nautique

Senior member
Jul 14, 2004
346
0
0
I have written code that allows me to talk to a server through a proxy, then the server sends the information back through the proxy to the browser. However only the text is showing. No pictures are transfered. Anyone know how to get the pictures to transfer as well. Do we have to use DataOutputStream/DataInputStream to transfer pictures? Any info would be great.
Thanks
 

znaps

Senior member
Jan 15, 2004
414
0
0
It's probably your mime-type. Is it being displayed as plain text, with no HTML formatting etc?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
GET http://www.google.com/ HTTP/1.0
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/html
Set-Cookie: PREF=ID=b9ffa141bec56fea:TM=1117656715:LM=1117656715:S=YLMT91qaBYB2_EG6; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.com
Server: GWS/2.1
Date: Wed, 01 Jun 2005 20:11:55 GMT
Connection: Close

<html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"><title>Google</title><style><!--
body,td,a,p,.h{font-family:arial,sans-serif;}
.h{font-size: 20px;}
.q{color:#0000cc;}
//-->
</style>
<script>
<!--
function sf(){document.f.q.focus();}
// -->
</script>
</head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onLoad=sf() topmargin=3 marginheight=3><center><img src="/intl/en/images/logo.gif" width=276 height=110 alt="Google"><br><br>
<form action=/search name=f><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>Web</b><a id=1a class=q href="/imghp?hl=en&tab=wi&ie=UTF-8">Images</a><a id=2a class=q href="http://groups-beta.google.com/grphp?hl=en&tab=wg&ie=UTF-8">Groups</a><a id=4a class=q href="/nwshp?hl=en&tab=wn&ie=UTF-8">News</a><a id=5a class=q href="/frghp?hl=en&tab=wf&ie=UTF-8">Froogle</a><a id=7a class=q href="/lochp?hl=en&tab=wl&ie=UTF-8">Local</a><b><a href="/options/" class=q>more&raquo;</a></b></font></td></tr
gt;</table><table cellspacing=0 cellpadding=0><tr><td width=25%></td><td align=center><input type=hidden name=hl value=en><input type=hidden name=ie value="ISO-8859-1"><input maxLength=256 size=55 name=q value=""><br><input type=submit value="Google Search" name=btnG><input type=submit value="I'm Feeling Lucky" name=btnI></td><td valign=top nowrap width=25%><font size=-2><a href=/advanced_search?hl=en>Advanced Search</a><br><a href=/preferences?hl=en>Preferences</a><br><a href=/language_tools?hl=en>Language class=q>more&raquo;</a></b></font></td></tr
gt;</table><tableTools</a></font></td></tr></table></form>&l
;br><br><font size=-1><a href="/ads/">AdvertisingPrograms</a> - <a href=/intl/en/about.html>About Google</a></font><p><font size=-2>&copy;2005 Google - Searching 8,058,044,651 web pages</font></p></center></body></html>
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
that is what i am getting back directly from the server....I don't see anything about a jpg or picture....not even the regular garbage you would get.

Any ideas?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Ok, I have just added the StringTokenizer object in my Proxy so that I could parse the webaddress that the user desires. When I did that, I now get the picture but it is all garbage information. I need to figure out to do that. do you have any suggestions on how I am to get it to display?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
No, I am not using Swing. I am using just a command prompt to display the header and other info and then displaying the web page back in the broswer. I have realized that I cannot use PrintWriter and BufferedReader to transport images because it puts it as garbage. So, I am trying to use BufferedInputStream and BufferedOutputStream. But the problem is that I do not know how to use the write() for the BufferedOutputStream because it requires that you pass it an int parameter. What is that int value supposed to be?
In my code I have a PrintWriter outputToServer. and when I send the info to the server I must do this:
outToServer.println(inputLine); // inputLine is the GET message
outToServer.println();

how do I do that same thing with the BufferedOutputStream?????
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Ok I have figured my last question. But I am now using the readLine method for a BufferedReader object and storing that into a string variable. I am then comparing that to null in a while loop, but my loop keeps getting hung up. I know that the header information is followed by a a blank line with no spaces and I do not know what to compare to it to get the while loop to exit. This is what I have.

while(( inputLine = inputFromClient.readLine()) != null)
{
do stuff
}

It is never exiting the while loop. I know that the readLine terminates from recieving a \n, \r or \r\n. But none of those are working. Any suggestions?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
When you send the server the GET phrase does it need all of the other stuff that goes along with it. My proxy will send all text and some pics then close. I am wondering if for some reason that would have to do with because I am only sending the server the Get phrase????
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I completely don't understand what you're asking anymore...

How many http requests (GETs) are you sending in total?
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Ok I am sending the GET message once to the server through a printwriter. I then read the information in from the server with DataInputStream and then send that to the browser using a DataOutputStream object. Don't worry about the questions before. Sorry I was thinking at the same time and trying to write.
My problem now is that I get all the text of the page, and only some of the pictures. Not all of the pictures will display. If i right click and click show picture it will show the picture. do you have any thoughts as to why all the pictures are not loading when they should be?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You do realize that the server will not send you an image unless you send it another request right? If the browser is showing any images it's probably because it's going to the server and getting it on it's own. The images that the browser does show probably have absolute urls to another server while the ones that don't show probably have relative urls so the browser is hitting your proxy trying to get them again.

I would highly recommend installing LiveHTTPHeaders (if you're using firefox) and ethereal for packet sniffing so that you can see exactly what is going over the network when.

Edit: and sorry about the not understanding thing. I forgot that you were the one who was writing the web proxy from that other thread :eek:
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
No problem.
Well I think that I am sending it all the requests it needs to get the entire page. This is what I do:

outToServer.println(webpage_desire); // outToServer = PrintWriter and webpage desired is the GET phrase
outToServer.println();

int num_bytes_stored;
while((num_bytes_stored = inToProxyFromServer.read()) != -1) inTo.. is a DataInputStream
{
outToclient.write(num_bytes_stored);
}

Then I close all the sockets and PrintWriters and look back around. All of my code is in a while loop so that it can continuously search for the webpages.
 

znaps

Senior member
Jan 15, 2004
414
0
0
Kamper's right I think. Think about what happens when the page contains a link for <img src="/images/whatever.gif">. What does your proxy do with that request?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
So how many times does that loop run (how many requests do you send to the server)? Once for the page and once again for each image?

Are you taking care to make sure that you always treat binary data as binary and text as text? Mixing chars and bytes is a bad thing. And you definitely don't want to treat your image data as characters.
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Well Im sending both text and pictures as bytes. Because I use DataOutputStream which deals with bytes to send the information from the server to the proxy and from the proxy to the browser.

I was told that should work but not every picture comes through. The test seems to be working fine. It's the pictures that aren't working all the time.