Results 1 to 2 of 2
- 11-28-2009, 09:35 AM #1
Member
- Join Date
- Nov 2009
- Posts
- 1
- Rep Power
- 0
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:
Besides, when I use Wireshark to capture packets, it looks all the same as what I get when I visit web sites via Firefox.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(); }
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.
And contents for header.txtJava 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); } } } }
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
- 11-28-2009, 04:23 PM #2
Similar Threads
-
append response to the request from Socket and write to another socket
By vaibhav_singh_vs@yahoo.co in forum NetworkingReplies: 3Last Post: 04-17-2009, 07:02 PM -
How can i know from which jsp request is coming?
By vishnujava in forum Java ServletReplies: 1Last Post: 08-06-2008, 01:13 PM -
First post as per request
By happyknappy in forum IntroductionsReplies: 3Last Post: 07-30-2008, 01:33 AM -
Session and request in JSF
By felixtfelix in forum Web FrameworksReplies: 0Last Post: 05-08-2008, 05:10 PM -
Help with request.getParameter()
By Albert in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-13-2007, 03:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks