公司在做个项目需要收集盛大的验证码,特意写了下面的代码,呵呵!几分钟保存了1万多张.如果人工保存工作量不敢想像! 不过有点搞笑,VB和C#都用上了!

1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using System.Net;
12
using System.Text;
13
using System.Net.Sockets;
14
using System.IO;
15
16
public partial class bb : System.Web.UI.Page
17

{
18
protected void Page_Load(object sender, EventArgs e)
19
{
20
IPHostEntry ipHostEntry = Dns.Resolve("esales.16288.com");
21
IPAddress address = ipHostEntry.AddressList[0];
22
IPEndPoint point = new IPEndPoint(address, 80);
23
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
24
try
25
{
26
client.Connect(point);
27
string s = "GET / HTTP/1.0/n Accept: */* /nUser-Agent: Mozilla/4.0.(compatible; MSIE.6.0; Windows NT 5.1) /n HOST: esales.16288.com /n Connection:Keep-Alive/n/n";
28
//string s = "GET /homepage.aspx HTTP/1.0/n User-Ageng: Mozilla/4.0.(compatible; MSIE.6.0; Windows NT 5.1) /n HOST: esales.16288.com /n Connection:Keep-Alive/n/n";
29
byte[] msgs = Encoding.Default.GetBytes(s);
30
client.Send(msgs);
31
byte[] bytes = new byte[4096];
32
33
int count;
34
string str = "";
35
count = client.Receive(bytes);
36
while (count > 0)
37
{
38
39
str += Encoding.Default.GetString(bytes, 0, count);
40
count = client.Receive(bytes);
41
}
42
43
//str是返回的内容
44
StreamWriter objSW = File.CreateText(Server.MapPath("body.txt"));
45
objSW.WriteLine(str);
46
objSW.Close();
47
48
}
49
catch (Exception ex)
50
{
51
52
}
53
finally
54
{
55
client.Shutdown(SocketShutdown.Both);
56
client.Close();
57
}
58
}
59
protected void Button1_Click(object sender, EventArgs e)
60
{
61
62
63
}
64
65
}
66

1
Imports System
2
Imports System.Data
3
Imports System.Configuration
4
Imports System.Collections
5
Imports System.Web
6
Imports System.Web.Security
7
Imports System.Web.UI
8
Imports System.Web.UI.WebControls
9
Imports System.Web.UI.WebControls.WebParts
10
Imports System.Web.UI.HtmlControls
11
Imports System.Net
12
Imports System.Text
13
Imports System.Net.Sockets
14
Imports System.IO
15
16
Public Class Default2Class Default2
17
Inherits System.Web.UI.Page
18
19
'{
20
Protected Sub Page_Load()Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
21
'{
22
23
'}
24
End Sub
25
26
Protected Sub Button1_Click()Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs)
27
28
'{
29
Dim i As Integer
30
For i = 0 To 1000
31
' Response.Write(GetCanshu("1") + "<br><br>")
32
' Response.Write(GetCanshu("2") + "<br>")
33
' Response.End()
34
35
Dim ipHostEntry As IPHostEntry = Dns.Resolve("esales.16288.com")
36
Dim address As IPAddress = ipHostEntry.AddressList(0)
37
Dim point As IPEndPoint = New IPEndPoint(address, 80)
38
Dim r As Random
39
r = New Random
40
Dim RanNum As String = r.Next(10000, 888888)
41
Dim client As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
42
Dim s1 As String = "GET /Validator/valid" + GetCanshu("2") + ".aspx?0." + RanNum + " HTTP/1.0" + vbCrLf + "User-Agent: Mozilla/4.0.(compatible; MSIE.6.0; Windows NT 5.1) " + vbCrLf + "HOST: esales.16288.com" + vbCrLf + "Cookie:ASP.NET_SessionID=" + GetCanshu("1") + ";path=/;HttpOnly" + vbCrLf + vbCrLf
43
' Response.Write(s1.Replace(Chr(13), "<br/>"))
44
Try
45
'{
46
client.Connect(point)
47
'Dim r As Random = New Random()
48
'Dim RanNum As String = r.Next(1123, 99999)
49
'//string s = "GET /bby/website/GetValidateCodeImage.aspx?Digits=4&Width=80&Height=20 HTTP/1.0/nHOST: 192.168.1.27 /nCookie:ASP.NET_SessionID="+TextBox1.Text.Trim()+"; path=/; HttpOnly/n/n";
50
51
Dim msgs As Byte() = Encoding.Default.GetBytes(s1)
52
53
client.Send(msgs)
54
Dim bytes(4096) As Byte
55
56
Dim count As Integer 'int ;
57
58
Dim str As String = ""
59
Dim stream As FileStream = New FileStream(Server.MapPath(New Random().Next(1, 99999).ToString + DateTime.Now.Ticks.ToString + " .gif"), FileMode.Create)
60
61
Dim writer As BinaryWriter = New BinaryWriter(stream)
62
63
count = client.Receive(bytes)
64
writer.Write(bytes, 224, count - 224)
65
While (count > 0)
66
' {
67
68
' //str += Encoding.UTF8.GetString(bytes, 0, count);
69
70
71
count = client.Receive(bytes)
72
writer.Write(bytes, 0, count)
73
'}
74
End While
75
76
77
78
writer.Close()
79
stream.Close()
80
81
82
'}
83
Catch ex As Exception
84
85
Response.Write(ex.Message)
86
Finally
87
' {
88
client.Shutdown(SocketShutdown.Both)
89
client.Close()
90
End Try
91
Next
92
'{
93
' //SendMessage(ex.Message.ToString() + "<EOF>");
94
'}
95
96
'}
97
98
'}
99
End Sub
100
Function GetCanshu()Function GetCanshu(ByVal a As String) As String
101
Dim str As String
102
Dim objStream As StreamReader = File.OpenText(Server.MapPath("body.txt"))
103
str = objStream.ReadToEnd()
104
objStream.Close()
105
'Dim a As String = "1"
106
If (a = "1") Then
107
'{
108
Dim array As Array ' string[] array;
109
array = Split(str, "SessionId=")
110
Dim canshu1 As String = Mid(array(1), 1, 24)
111
GetCanshu = canshu1
112
' Response.Write(canshu1)
113
'}
114
Exit Function
115
Else
116
' {
117
Dim array1 As Array ' string[] array1;
118
array1 = Split(str, "Validator/valid")
119
Dim canshu2 As String = Mid(array1(1), 1, 7)
120
GetCanshu = canshu2
121
' Response.Write(canshu2)
122
Exit Function
123
End If
124
End Function
125
126
127
128
129
End Class
http://www.cnblogs.com/dotnetblog/archive/2006/11/10/556956.html