用.net编写站内短信群发软件,主要有几个问题
- 分析http通信协议。摸透登录、发消息的通信细节
- 实现登录并保持登录状态
- 发送消息
看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
namespace pceggs
{
class Program
{
static void Main( string [] args)
{
SRWebClient client = new SRWebClient();
client.DownloadHtml( " http://www.pceggs.com/Login.aspx " , true );
string jpg = System.Environment.CurrentDirectory + " // " + Guid.NewGuid().ToString() + " .jpg " ;
client.DownloadFile( " http://www.pceggs.com/CheckCode.aspx " , jpg, false );
System.Diagnostics.Process.Start(jpg);
Console.WriteLine( " 输入验证码: " );
string code = Console.ReadLine();
string username, password, msg;
int start, end, sleep;
if (System.IO.File.Exists(System.Environment.CurrentDirectory + " //config.txt " ))
{
using (StreamReader sr = new StreamReader(System.Environment.CurrentDirectory + " //config.txt " ))
{
username = sr.ReadLine();
password = sr.ReadLine();
start = int .Parse(sr.ReadLine());
end = int .Parse(sr.ReadLine());
sleep = int .Parse(sr.ReadLine());
msg = sr.ReadToEnd();
sr.Close();
}
}
else
{
Console.WriteLine( " 输入用户名: " );
username = Console.ReadLine();
// username = " http://www.svnhost.cn ";
// 作者:小灰
// 网站: http://www.svnhost.cn
Console.WriteLine( " 输入密码: " );
password = Console.ReadLine();
Console.WriteLine( " 输入开始号码: " );
start = int .Parse(Console.ReadLine());
Console.WriteLine( " 输入结束号码: " );
end = int .Parse(Console.ReadLine());
Console.WriteLine( " 输入发送间隔(毫秒): " );
sleep = int .Parse(Console.ReadLine());
Console.WriteLine( " 输入发送内容:(可以实现写好,粘贴过来。内容中不能换行) " );
msg = Console.ReadLine();
}
RequestData data = new RequestData();
data.AddField( " input_rturl " , "" );
data.AddField( " Tuserid " , username);
data.AddField( " password " , password);
data.AddField( " CheckCode " , code);
data.AddField( " Submit.x " , " 52 " );
data.AddField( " Submit.y " , " 9 " );
string html = client.Request( " http://www.pceggs.com/LoginDo.aspx " , data, true );
Console.WriteLine(html);
Console.WriteLine( " Ctrl+Z 退出,回车后开始发送 " );
Console.ReadLine();
RequestData d1 = new RequestData();
d1.AddField( " x " , " 47 " );
d1.AddField( " y " , " 10 " );
// html = client.DownloadHtml(" http://www.pceggs.com/sendmsg.aspx?userid=8889370 ", true);
while (start <= end)
{
d1.AddField( " userid " , start.ToString());
d1.AddField( " REUSERID " , start.ToString());
d1.AddField( " SUB " , msg);
html = client.Request( " http://www.pceggs.com/sendmsgdo.aspx " , d1, false );
if (html.Contains( " 发送成功 " ))
Console.WriteLine(start.ToString() + " 发送成功, " + sleep.ToString() + " 毫秒后发送下一个 " );
else
Console.WriteLine(start.ToString() + " 发送失败: " + html + sleep.ToString() + " 毫秒后发送下一个 " );
start ++ ;
Thread.Sleep(sleep);
}
Console.WriteLine( " 发送完成。 " );
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;
using System.IO;
using System.Collections;
namespace pceggs
{
public class SRWebClient
{
public string cookie = null ;
public SRWebClient()
{
}
/**/
/// <TgData>
/// <Alias> 下载Web源代码 </Alias>
/// </TgData>
public string DownloadHtml( string URL, bool CreateCookie)
{
try
{
HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;
if (cookie != null )
{
request.Headers.Add( " Cookie " , cookie);
}
request.AllowAutoRedirect = false ;
// request.MaximumAutomaticRedirections = 3;
request.Timeout = 20000 ;
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
string r = "" ;
System.IO.StreamReader S1 = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.UTF8);
try
{
r = S1.ReadToEnd();
if (CreateCookie)
cookie = res.Headers[ " Set-Cookie " ];
}
catch (Exception er)
{
// Log l = new Log();
// l.writelog("下载Web错误", er.ToString());
}
finally
{
res.Close();
S1.Close();
}
return r;
}
catch
{
}
return string .Empty;
}
/**/
/// <TgData>
/// <Alias> 下载文件 </Alias>
/// </TgData>
public long DownloadFile( string FileURL, string FileSavePath, bool CreateCookie)
{
long Filelength = 0 ;
HttpWebRequest req = HttpWebRequest.Create(FileURL) as HttpWebRequest;
if (cookie != null )
{
req.Headers.Add( " Cookie " , cookie);
}
req.AllowAutoRedirect = true ;
req.Referer = " http://www.pceggs.com/Login.aspx " ;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
if (CreateCookie)
cookie = res.Headers[ " Set-Cookie " ];
System.IO.Stream stream = res.GetResponseStream();
try
{
Filelength = res.ContentLength;
byte [] b = new byte [ 512 ];
int nReadSize = 0 ;
nReadSize = stream.Read(b, 0 , 512 );
System.IO.FileStream fs = System.IO.File.Create(FileSavePath);
try
{
while (nReadSize > 0 )
{
fs.Write(b, 0 , nReadSize);
nReadSize = stream.Read(b, 0 , 512 );
}
}
finally
{
fs.Close();
}
}
catch (Exception er)
{
// Log l = new Log();
// l.writelog("下载文件错误", er.ToString());
}
finally
{
res.Close();
stream.Close();
}
return Filelength;
}
/**/
/// <TgData>
/// <Alias> 提交数据 </Alias>
/// </TgData>
public string Request( string RequestPageURL, RequestData Data, bool CreateCookie)
{
StreamReader reader = null ;
HttpWebResponse response = null ;
HttpWebRequest request = null ;
try
{
string StrUrl = RequestPageURL;
request = HttpWebRequest.Create(StrUrl) as HttpWebRequest;
string postdata = Data.GetData();
request.Referer = RequestPageURL;
request.AllowAutoRedirect = false ;
request.UserAgent = " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727) " ;
request.Timeout = 20000 ;
if (cookie != null )
{
request.Headers.Add( " Cookie " , cookie);
}
Uri u = new Uri(StrUrl);
if (postdata.Length > 0 ) // 包含要提交的数据 就使用Post方式
{
request.ContentType = " application/x-www-form-urlencoded " ; // 作为表单请求
request.Method = " POST " ; // 方式就是Post
// 把提交的数据换成字节数组
Byte[] B = System.Text.Encoding.UTF8.GetBytes(postdata);
request.ContentLength = B.Length;
System.IO.Stream SW = request.GetRequestStream(); // 开始提交数据
SW.Write(B, 0 , B.Length);
SW.Close();
}
response = request.GetResponse() as HttpWebResponse;
if (CreateCookie)
cookie = response.Headers[ " Set-Cookie " ];
reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}
catch (Exception ex)
{
string x = ex.StackTrace;
}
finally
{
if (response != null )
response.Close();
}
return string .Empty;
}
public bool PostDownload(RequestData Data, out string file)
{
file = null ;
StreamReader reader = null ;
HttpWebResponse response = null ;
HttpWebRequest request = null ;
try
{
string StrUrl = " http://www.imobile.com.cn/wapdiyringdownload.php " ;
request = HttpWebRequest.Create(StrUrl) as HttpWebRequest;
string postdata = Data.GetData();
request.Referer = StrUrl;
request.AllowAutoRedirect = false ;
request.UserAgent = " Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727) " ;
request.Timeout = 20000 ;
if (cookie != null )
{
request.Headers.Add( " Cookie " , cookie);
}
Uri u = new Uri(StrUrl);
if (postdata.Length > 0 ) // 包含要提交的数据 就使用Post方式
{
request.ContentType = " application/x-www-form-urlencoded " ; // 作为表单请求
request.Method = " POST " ; // 方式就是Post
// 把提交的数据换成字节数组
Byte[] B = System.Text.Encoding.UTF8.GetBytes(postdata);
request.ContentLength = B.Length;
System.IO.Stream SW = request.GetRequestStream(); // 开始提交数据
SW.Write(B, 0 , B.Length);
SW.Close();
}
response = request.GetResponse() as HttpWebResponse;
string des = response.Headers[ " Content-Disposition " ].Trim();
file = des.Substring(des.IndexOf( " filename= " ) + 9 );
file = new Random().Next( 100 ).ToString() + " / " + file;
System.IO.Stream stream = response.GetResponseStream();
try
{
int Filelength = ( int )response.ContentLength;
byte [] b = new byte [ 512 ];
int nReadSize = 0 ;
nReadSize = stream.Read(b, 0 , 512 );
System.IO.FileStream fs = System.IO.File.Create( " f:/mobileMusic/ " + file);
try
{
while (nReadSize > 0 )
{
fs.Write(b, 0 , nReadSize);
nReadSize = stream.Read(b, 0 , 512 );
}
}
finally
{
fs.Close();
}
}
catch (Exception er)
{
// Log l = new Log();
// l.writelog("下载文件错误", er.ToString());
}
finally
{
response.Close();
stream.Close();
}
}
catch (Exception ex)
{
string x = ex.StackTrace;
}
finally
{
if (response != null )
response.Close();
}
return true ;
}
#region GetPage
/// <summary>
/// 获取源代码
/// </summary>
/// <param name="url"></param>
/// <param name="coding"></param>
/// <param name="TryCount"></param>
/// <returns></returns>
public static string GetPage( string url, Encoding encoding, int TryCount)
{
for ( int i = 0 ; i < TryCount; i ++ )
{
string result = GetPage(url, encoding);
if (result != null && result != string .Empty)
return result;
}
return string .Empty;
}
/// <summary>
/// 获取源代码
/// </summary>
/// <param name="url"></param>
/// <param name="coding"></param>
/// <returns></returns>
public static string GetPage( string url, Encoding encoding)
{
HttpWebRequest request = null ;
HttpWebResponse response = null ;
StreamReader reader = null ;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 20000 ;
request.AllowAutoRedirect = false ;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024 )
{
reader = new StreamReader(response.GetResponseStream(), encoding);
string html = reader.ReadToEnd();
return html;
}
}
catch
{
}
finally
{
if (response != null )
{
response.Close();
response = null ;
}
if (reader != null )
reader.Close();
if (request != null )
request = null ;
}
return string .Empty;
}
#endregion
}
public class RequestData
{
Hashtable hash = new Hashtable();
public RequestData()
{
}
public string GetData()
{
string r = "" ;
foreach ( string key in hash.Keys)
{
if (r.Length > 0 ) r += " & " ;
r += key + " = " + hash[key];
}
return r;
}
public void AddField( string Field, string Value)
{
hash[Field] = Value;
}
}
}
config.txt内容如下:
quxiaohui_0@163.com
111111
88899889
888998
40000