• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Setting the browser name of a javabean

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all; sorry for the bad title, I don't know what to call it

My problem is this: I have a javabean which is used to read pages
from websites and I want this bean to tell the host what the "browser name" is. My current code doesn't set any such value on the host other than null.


/*****************************************************************/


public String readPage(String strHost, int intPort, String strFile)
{
String strContent = "";

//Read content from page
try
{
Socket socket = new Socket(strHost, intPort);

BufferedReader inputStream = new BufferedReader(
new InputStreamReader(socket.getInputStream()));

PrintWriter outputStream = new PrintWriter(
new OutputStreamWriter(socket.getOutputStream()),true);

outputStream.println("GET /" + strFile);

// I guess this is where the action must happen?

String strLine = "";

while((strLine = inputStream.readLine()) != null)
{
strContent += strLine + "\n";
}

socket.close();
}
catch(IOException ioe)
{
Debug.print("IOException", this);
}

return strContent;
}


/******************************************************************/


Any help much appreciated.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you're talking about the User-Agent: HTTP header, which tells the Web server what browser you're ostensibly using. To include that, you'll have to send HTTP/1.0 commands instead of the HTTP/0.9 you're using now -- and then things get complicated.

Why not use java.net.URLConnection to fetch pages? It includes all sorts of nifty features, including the ability to set header values like this one.
 
Mahoney Futhark
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried sending the user-agent in header, but failed. I'll look up on the HTTP/1.0 thing and try again. Thanks for the tip.

If that fails I'll have a go at java.net.URLConnection.
 
Mahoney Futhark
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success! It really wasn't all that hard, but I definitely needed some help looking in the right direction.

I changed a line of the code listed in the first post to:


outputStream.println("GET /" +
strFile +
" HTTP/1.0\r\n" +
"User-Agent: " +
"MySlimyBot/0.0a http://www.xyzdomain-name.com/MySlimyBot.htm\r\n");



Thanks again.
[ November 15, 2004: Message edited by: Mahoney Futhark ]
 
These are the worst of times and these are the best of times. And this is the best tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic