Results 1 to 4 of 4
  1. #1
    DaBernie is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Smile write http response into file?

    Hi everybody,

    I want to know how it's possible to write
    a http responsen into a file.
    I tried it a little, but I had a lot of conversion problem:

    Here is the code:
    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class Studium {
      public static void main(String[] args) throws IOException{
    	
    	if(args.length == 0){throw new IllegalArgumentException("define host!");}
    	
    	OutputStream response = null;
    	if (args.length == 2) response = new FileOutputStream(args[1]);
        else response = System.out;
    	URL url = new URL(args[0]);
    	String protocol = url.getProtocol();
    	if(!protocol.equals("http")){
    		throw new IllegalArgumentException("Please Enter the complete URL which includes protocol");}
    	String host = url.getHost();
    	int port = 80;	
    
    String[] Test={
    		"String1",
    		"String2",
    		"String3"};
    
    
    try {
    		for(int i = 0; i < Test.length; i++)
    			{
    			Socket socket = new Socket(host, port);
    			InputStream input = socket.getInputStream();
    			OutputStream request = socket.getOutputStream();
    			String s = "GET /" + Test[i] + "\n";
    			request.write(s.getBytes()); 
    			request.flush();
    			
    			byte[] buffer = new byte[4096];
    			int bytes;
    			while((bytes = input.read(buffer)) != -1){
    			response.write(buffer, 0, bytes);
    			}
    			
    			request.close();
    			input.close();
    			socket.close();
    
    		}
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	}    
      }
    thank you very much :)

  2. #2
    DaBernie is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default or

    If you have a clue how to get
    the response statuscode with this code
    and only a few modifications,
    it would also solve my problem.

  3. #3
    DaBernie is offline Member
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    nenene dieses forum is mir zu schlecht.
    ich komme nicht mehr hier ihr kotnascher.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Specify what you mean by you got conversion problems?
    P.S Close all those streams in a finally block.

Similar Threads

  1. Server returned HTTP response code: 500.. i need help
    By hardc0d3r in forum Java Servlet
    Replies: 9
    Last Post: 03-12-2012, 08:08 PM
  2. Getting information about HTTP response method
    By Shiv in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 07:58 PM
  3. HTTP response not read
    By SD_Be in forum Networking
    Replies: 1
    Last Post: 03-10-2009, 04:36 PM
  4. Http Webserver - Mage inclution in response
    By nlgopi in forum Networking
    Replies: 3
    Last Post: 10-18-2008, 03:17 PM
  5. Server returned HTTP response code: 500
    By Heather in forum Java Servlet
    Replies: 1
    Last Post: 07-09-2007, 04:32 AM

Posting Permissions

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