Results 1 to 2 of 2
- 10-09-2009, 04:30 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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.
what should I do? :(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(); } }Last edited by DaBernie; 10-09-2009 at 11:39 PM. Reason: changing the status of the thread
- 10-09-2009, 11:42 PM #2
Member
- Join Date
- Oct 2009
- Posts
- 5
- Rep Power
- 0
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
-
Program doesn't continue...
By Reiyn in forum New To JavaReplies: 7Last Post: 10-07-2008, 10:28 PM -
press any key to continue
By dotnet007 in forum New To JavaReplies: 3Last Post: 05-11-2008, 05:19 AM -
How to use Continue with a label
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM -
How to use Continue
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks