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

    Unhappy GET / + Problem to continue with stringfile

    Hi everyone,

    I habe a Problem: I want to read out a textfile
    which looks like:
    string1
    string2
    ...
    and add this string to GET /
    but I'm only able to request the first string to the server
    the rest doesn't work.
    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;
    	Socket socket = new Socket(host, port);
    	InputStream input = socket.getInputStream();
    	PrintWriter request = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
    	
    	try {
    		BufferedReader in = new BufferedReader(new FileReader("list2.db"));
    		String line = null;
    		@SuppressWarnings("unused")
    		int counter = 0;
    		while ((line = in.readLine()) != null) {
    			request.println("GET /" + line); 
    			request.flush();
    			byte[] buffer = new byte[4096];
    			int bytes;
    			while((bytes = input.read(buffer)) != -1){
    				response.write(buffer, 0, bytes);
    			}
    			
    
    		}
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	
    	socket.close();
    	response.close();
    	
    	}    
      }
    what should I do? :(
    Last edited by DaBernie; 10-09-2009 at 11:39 PM. Reason: changing the status of the thread

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

    Default

    Hi everybody,

    I have solved it.
    This code above this post is very chaotic :D
    I cleaned up... aand, I think for every request you have to create a new Socket
    (Of course you should close the old socket) and it works :)

    new 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();
    	}
    	}    
      }

Similar Threads

  1. Program doesn't continue...
    By Reiyn in forum New To Java
    Replies: 7
    Last Post: 10-07-2008, 10:28 PM
  2. press any key to continue
    By dotnet007 in forum New To Java
    Replies: 3
    Last Post: 05-11-2008, 05:19 AM
  3. How to use Continue with a label
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 PM
  4. How to use Continue
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:46 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
  •