朋友让我帮忙做个刷票程序,原来打算从网上找个程序给他就算了,但登陆那个网站发现验证码是直接生成的文字...网上找到的都是对图片的处理....
想想其实,自己也可以做一个刷票程序...
于是写了以下代码,基本是参照网上的资料和api帮助文档....可惜最后对代理处理还是没成功,总是连接失败.....
想想其实,自己也可以做一个刷票程序...
于是写了以下代码,基本是参照网上的资料和api帮助文档....可惜最后对代理处理还是没成功,总是连接失败.....
import java.io.*;
import java.net.*;
public class shuapiao {
String myurl="";//投票填写的url
String sub_gdly="http://";//投票处理的url
String session_id="";
String id="";
String proxyAddr="82.205.237.26";
int proxyPost=8080;
//HttpURLConnection conn = (HttpURLConnection)url.openConnection();
public shuapiao(String myurl){
this.myurl=myurl;
}
public String getUrl(){
String valid="";
try {
InputStream in;
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress
(this.proxyAddr,this.proxyPost));
URL url=new URL(myurl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();//代理总连接不上,所以没用
//int size = conn.getHeaderFields().size();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.setRequestProperty("host","www");//主机地址
conn.setRequestProperty("Connection","keep-alive");//长连接,这个很好用!
//HttpURLConnection connection = (HttpURLConnection)url.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.connect();
in = conn.getInputStream();
java.io.BufferedReader breader =
new BufferedReader(new InputStreamReader(in , "GBK"));
String str=breader.readLine();
while(str != null){
System.out.println(str);
if(str.indexOf("验证码:<input type=\"text\" name=\"verifycode\" size=\"4\" /><font color=
\"#0000FF\" face=\"Georgia, Times New Roman, Times, serif\" size=\"3\"><i><b>")>-1){
valid=str.substring(str.indexOf("<b>")+3, str.indexOf("</b>"));
}
str=breader.readLine();
}
String cookieval = conn.getHeaderField("set-cookie");
System.out.println(cookieval);
String sessionid=null;
if(cookieval != null){
sessionid=cookieval.substring(0,cookieval.indexOf(";"));
}
System.out.println(sessionid);
this.session_id=sessionid+"&path=/";
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return valid;
}
public void postURL(){
try {
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP,new InetSocketAddress
(this.proxyAddr,this.proxyPost));
URL url=new URL(sub_gdly);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
conn.setRequestProperty("host","www.163.com");
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty("cookie", session_id.substring(0,session_id.indexOf("&")));
StringBuffer sb=new StringBuffer();
sb.append
("name=01&name=10&name=02&name=03&name=04&name=05&name=06&name=24&name=07&name=08&name1=hhhf&sex=
%C4%D0&tele=111111111&comp=111111&addr=11111&postcode=010000&verifycode=" +
this.id+"&B1=%CC%E1%BD%BB");
System.out.println(sb.toString());
OutputStream os=conn.getOutputStream();
os.write(sb.toString().getBytes("GBK"));
os.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
conn.connect();
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String args[]){
for(int i=0;i<10;i++){
shuapiao u=new shuapiao("http://");
u.id=u.getUrl();
u.postURL();
}
}
}