Results 1 to 2 of 2
  1. #1
    stoundmire is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Unable to request more than once using one socket

    At first, I create a socket and send various HTTP requests using this socket. But only the first request is responsed.
    The behavior of the code is someting like this:
    Java Code:
      Socket s=new Socket(host,port);
      PrintWriter out=new PrintWriter(s.getOutputStream());
      for every request{
        print the line "GET|POST uri HTTP/1.1"
        output request headers
        [output form data]
        print an empty line
        out.flush();
      }
    Besides, when I use Wireshark to capture packets, it looks all the same as what I get when I visit web sites via Firefox.

    But when I create seperate sockets for every request and then close them, it works.
    This is my question, please help me. Thank you.

    Code for PlainCookie.java
    Hint: Whether create seperate sockets for every request is determined by the variable 'ev', which is set in the constructor.
    Java Code:
    import java.net.*;
    import java.io.*;
    import java.util.*;
    
    public class PlainCookie {
    	private List<String> header;
    	private List<Pair> cookie;
    	private Socket s=null;
    	private String url;
    	private int port;
    	private boolean ev=true;
    	public PlainCookie(String url,int port,boolean ev)throws Exception{
    		header=new LinkedList<String>();
    		cookie=new LinkedList<Pair>();
    		this.url=url;
    		this.port=port;
    		this.ev=ev;
    		if(!ev)
    			s=new Socket(url,port);
    	}
    	public PlainCookie(String url,int port)throws Exception{
    		this(url,port,false);
    	}
    	public void addHeader()throws Exception{
    		BufferedReader in=new BufferedReader(new FileReader("header.txt"));
    		String line;
    		while((line=in.readLine())!=null){
    			header.add(line);
    		}
    		in.close();
    	}
    	public void visit(String file,String post)throws Exception{
    		if(ev)
    			s=new Socket(url,port);
    		PrintWriter out=new PrintWriter(s.getOutputStream());
    		out.print(post==null?"GET":"POST");
    		out.println(" /"+file+" HTTP/1.0");
    		for(int k=0;k<header.size();++k)
    			out.println(header.get(k));
    		if(cookie.size()!=0){
    			out.print("Cookie:");
    			for(int k=0;k<cookie.size();++k){
    				out.print(" "+cookie.get(k).name+"="+cookie.get(k).value);
    				if(k<cookie.size()-1)
    					out.print(";");
    			}
    			out.println();
    		}
    		if(post!=null){
    			out.println("Content-Type: application/x-www-form-urlencoded");
    			out.println("Content-Length: "+post.length());
    			out.println();
    			out.print(post);
    		}
    		else{
    			out.println();
    		}
    		out.flush();
    		out.flush();
    		BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
    		String line,body;
    		StringBuilder sb=new StringBuilder();
    		while((line=in.readLine())!=null){
    			if(line.length()==0)
    				break;
    			if(line.startsWith("Set-Cookie")){
    				line=line.substring("Set-Cookie: ".length());
    				StringTokenizer st1=new StringTokenizer(line,"; ");
    				while(st1.hasMoreTokens()){
    					String pt=st1.nextToken(),name,value;
    					if(pt.startsWith("path"))
    						continue;
    					StringTokenizer st2=new StringTokenizer(pt,"=");
    					name=st2.nextToken();
    					value=st2.nextToken();
    					cookie.add(new Pair(name,value));
    				}
    			}
    		}
    		while((line=in.readLine())!=null){
    			sb.append(line);
    			sb.append('\n');
    		}
    		body=sb.toString();
    		System.out.println(body.indexOf("Nobody")+"\t"+body.indexOf("stoundmire"));
    		if(ev)
    			s.close();
    	}
    	public void close()throws Exception{
    		if(!ev)
    			s.close();
    	}
    	class Pair{
    		String name;
    		String value;
    		public Pair(String name,String value){
    			this.name=name;
    			this.value=value;
    		}
    	}
    	public static void main(String[] args){
    		PlainCookie hp=null;
    		try{
    			hp=new PlainCookie("acm.hrbeu.edu.cn",80,true);
    			hp.addHeader();
    			hp.visit("index.php",null);
    			Thread.sleep(500);hp.visit("index.php?act=cp",null);
    		}
    		catch(Exception ex){
    			ex.printStackTrace(System.err);
    		}
    		finally{
    			try{
    				if(hp!=null)
    					hp.close();
    			}
    			catch(Exception ex){
    				ex.printStackTrace(System.err);
    			}
    		}
    	}
    }
    And contents for header.txt
    Java Code:
    Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
    Accept-Language: zh-cn
    UA-CPU: x86
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0
    Host: acm.hrbeu.edu.cn
    Connection: Keep-Alive

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    you appear to be explicitly closing the socket at the end of visit();

    Java Code:
    private boolean ev=true;
    //...
    
    public void visit(String file,String post)throws Exception{
    //...
      if(ev)
        s.close();
    }

Similar Threads

  1. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  2. How can i know from which jsp request is coming?
    By vishnujava in forum Java Servlet
    Replies: 1
    Last Post: 08-06-2008, 02:13 PM
  3. First post as per request
    By happyknappy in forum Introductions
    Replies: 3
    Last Post: 07-30-2008, 02:33 AM
  4. Session and request in JSF
    By felixtfelix in forum Web Frameworks
    Replies: 0
    Last Post: 05-08-2008, 06:10 PM
  5. Help with request.getParameter()
    By Albert in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-13-2007, 04:39 PM

Tags for this Thread

Posting Permissions

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