Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2009, 10:35 AM
Member
 
Join Date: Nov 2009
Posts: 1
Rep Power: 0
stoundmire is on a distinguished road
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:
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.
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
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-28-2009, 05:23 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 280
Rep Power: 1
travishein is on a distinguished road
Default
you appear to be explicitly closing the socket at the end of visit();

Code:
private boolean ev=true;
//...

public void visit(String file,String post)throws Exception{
//...
  if(ev)
    s.close();
}
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
socket

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +2. The time now is 02:19 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org