Need to write to browser from proxy

imported_nautique

Senior member
Jul 14, 2004
346
0
0
I have created a proxy that is connected to the browser and to the internet. I have set it up where I can block webpages and I need to display a stream of text saying: "403 Forbidden" or something.
I have created a BufferedWriter blocked = new BufferedWriter(new PrintWriter(outToclient)); // outToclient is a DataOutputStream
I then say blocked_message403.write(forbidden_message, 0, forbidden_message.length());
But this is just displaying a blank page with no text. What am I doing wrong? Any suggestions?
Thanks
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Also, it doesn't have to be the message that the HTTP spits out. It can be just plain text. How can I send just a string from the proxy to display in the browser????
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You can still provide content with a 403 code. Just send back a page like normal, but write the html yourself and have it explain about the error. (And don't forget the Content-Length: header ;))
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Ok I created an HTML or txt document that included the following:

HTTP/1.0 403 Forbidden
Content-Type: text/html

<html>
<head></head>
<body>
Forbidden: You do not have access to view this page.
</body>
</html>
_________________________________________________________

I then create a bufferedreader object.
I then use a while(data = read_forbidden_txt.readLine()) ! = null)
outToclient.writeBytes(data); // outToclient is a DataOutputStream

I am getting a blank screen. No text. Why

 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Because the browser has no idea if you're done sending data or not. You need a Content-Length header (there's another way but it's more complicated and unneccesary when you've got the whole response in memory like that).
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
HTTP/1.0 403 Forbidden
Content-Length: 13644
Content-Type: text/html
Cache-Control: private
Set-Cookie: PR
Server: GWS/2.1
Date: Thu, 09 Jun 2005 01:21:48 GMT
Connection: Close


<html>
<title>Dan</title>
<head>Hello</head>
<body>
Forbidden: This page has been blocked by your browser. Contact Admin for further discussion.
</body>
</html>

This is how I changed it but still nothing. I have tried searching google for Content-Length and this is how it is too. Anything else I am leaving out?
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
You might try using a packet sniffer to see what exactly the browser is receiving.
 

imported_nautique

Senior member
Jul 14, 2004
346
0
0
Well I am printing out the file with that specified code when I send it out to make sure its receiving it all. And it does. What do you mean by packet sniffer?
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Why did you set Content-Length to 13644?

I count 166 (single byte per character and two bytes per \n). Since you've been writing character data to a byte stream I suspect you'll get two bytes per character (or whatever java's unicode uses). You could try setting the length to something smaller (like 166) and see if the browser gets the document or cuts it short.

A packet sniffer is a program that records every byte that passes through your network card and analyzes it. You can use it to analyze exactly what bytes you're sending over your sockets so you can judge how many characters are in your message.