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 |