Results 1 to 3 of 3
  1. #1
    JavaVox is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Question How do you modify http header?

    Hi

    I am using the following code to connect to different web sites but some will accept the connection
    and some won't. I believe that the ones that won't will not do it because the JVM is not sending
    the correct header info.

    Java Code:
    Vector vect = new Vector(); 
               String filename = "htmpage.html";
    
               String url = "http://www.genericwebsite.com";
              
    
              // THE NEXT CODE TRYS TO LOOK UP PAGE AND SAVE IN A FILE
    
    
                 URL myurl = new URL(url);
    
                 FileWriter fw = new FileWriter(filename);
                
                 BufferedReader in = new BufferedReader(
                 new InputStreamReader(
                 myurl.openStream()));
    
                 String inputLine;
    
                 while ((inputLine = in.readLine()) != null){
                   vect.addElement(inputLine);
                   fw.write(inputLine);
                 }
    
                 fw.close();
    I believe that one of the things I have to do is set the User-Agent but don't know how to do this.
    Can anyone help? Thanks in advance.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Modify the header by setting the request property with the urlConnection object retrieved by calling URL.getConnection()
    Java Code:
    URLConnection conn = myurl.getConnection();
    conn.setRequestProperty("mykey", "myvalue");

  3. #3
    JavaVox is offline Member
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. modify XML file
    By Fireholder in forum Advanced Java
    Replies: 1
    Last Post: 04-19-2011, 09:06 AM
  2. http://localhost:8080/helloservice/HelloService?Tester HTTP Status 404 -
    By vietnamusa in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 03-13-2011, 12:02 AM
  3. Modify JOptionPane
    By GodSon in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2010, 03:46 PM
  4. Replies: 7
    Last Post: 11-11-2009, 08:29 PM
  5. Modify A* Algorithm
    By prakharbirla in forum Advanced Java
    Replies: 1
    Last Post: 02-13-2008, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •