admin 管理员组

文章数量: 887007

第六讲 smart qq C#开发总结

smart qqC#开发总结:

整个开发下来其实一点都不是很难,从一开始二维码 获取到最终的收发消息,基本上都是模拟浏览器的操作。都是基于http通讯。一下就是 本次新手学习http协议的最关键的一个类

/// <summary>
/// HTTP网络通信类
/// </summary>
public static class HTTP
{

public static CookieContainer cookies = new CookieContainer();
static CookieCollection CookieCollection = new CookieCollection();
static CookieContainer CookieContainer = new CookieContainer();
/// <summary>
/// HTTP请求
/// </summary>
/// <param name="url">连接</param>
/// <param name="enc">编码</param>
/// <returns></returns>
public static string HttpGet(string url, Encoding enc)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 10000;//设置10秒超时
request.Proxy = null;
request.Method = "GET";
request.ContentType = "application/x-www-from-urlencoded";
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), enc);
StringBuilder sb = new StringBuilder();
sb.Append(reader.ReadToEnd());
reader.Close();
reader.Dispose();
response.Close();
return sb.ToString();
}
/// <summary>
/// 带referer的http请求连接
/// </summary>
/// <param name="url"></param>
/// <param name="referer"></param>
/// <param name="timeout"></param>
/// <param name="encode"></param>
/// <param name="NoProxy"></param>
/// <returns></returns>
public static string Get(string url, string referer = ".html?v=20151105001&callback=1&id=2", int timeout = 100000, Encoding encode = null, bool NoProxy = false)
{
string dat;
HttpWebResponse res = null;
HttpWebRequest req;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cookies;
req.AllowAutoRedirect = false;
req.Timeout = timeout;
req.Referer = referer;
if (NoProxy)
req.Proxy = null;
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
res = (HttpWebResponse)req.GetResponse();

cookies.Add(res.Cookies);
}
catch (HttpException)
{
return "";
}
catch (WebException)
{
return "";
}
StreamReader reader;

reader = new StreamReader(res.GetResponseStream(), encode == null ? Encoding.UTF8 : encode);
dat = reader.ReadToEnd();

res.Close();
req.Abort();

return dat;
}
/// <summary>
/// 带referer data的http post
/// </summary>
/// <param name="url"></param>
/// <param name="data"></param>
/// <param name="Referer"></param>
/// <param name="timeout"></param>
/// <param name="encode"></param>
/// <returns></returns>
public static string Post(string url, string data, string Referer = ".html?v=20151105001&callback=1&id=2",string method= "POST", int timeout = 100000, Encoding encode = null)
{
string dat = "";
HttpWebRequest req;
try
{
req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = cookies;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = method;
req.Proxy = null;
req.Timeout = timeout;
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
req.ProtocolVersion = HttpVersion.Version10;
req.Referer = Referer;

byte[] mybyte = Encoding.Default.GetBytes(data);
req.ContentLength = mybyte.Length;

Stream stream = req.GetRequestStream();
stream.Write(mybyte, 0, mybyte.Length);


HttpWebResponse res = req.GetResponse() as HttpWebResponse;

cookies.Add(res.Cookies);
stream.Close();

StreamReader SR = new StreamReader(res.GetResponseStream(), encode == null ? Encoding.UTF8 : encode);
dat = SR.ReadToEnd();
res.Close();
req.Abort();
}
catch (HttpException)
{
return "";
}
catch (WebException)
{
return "";
}

return dat;
}
/// <summary>
/// Hash 转换
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int Hash33(string s)
{
var e = 0;
foreach (var t in s)
e += (e << 5) + t;
return int.MaxValue & e;
}
/// <summary>
/// 获取CookieCollection下 所有cookie
/// </summary>
/// <param name="cc"></param>
/// <returns></returns>
public static string GetAllCookies(CookieCollection cc)
{
//List<Cookie> lstCookies = new List<Cookie>();
string lstCookies = "";
ArrayList table = (ArrayList)cc.GetType().InvokeMember("m_list",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance, null, cc, new object[] { });

if (table != null)
{
for (int i = 0; i < table.Count; i++)
{
string[] tacookie = table[i].ToString().Split('=');
lstCookies = tacookie[1].ToString().Trim();
}
}


return lstCookies;
}
public delegate void Post_Async_Action(string data);
private class Post_Async_Data
{
public HttpWebRequest req;
public Post_Async_Action post_Async_Action;
}
public static void Post_Async(string url, string PostData, Post_Async_Action action, string Referer = ".html?v=20151105001&callback=1&id=2", int timeout = 100000)
{
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;
req.CookieContainer = cookies;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.Referer = Referer;
req.UserAgent = "Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0) Gecko/20100101 Firefox/47.0";
req.Proxy = null;
req.ProtocolVersion = HttpVersion.Version10;
req.ContinueTimeout = timeout;

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(PostData);
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

Post_Async_Data dat = new Post_Async_Data();
dat.req = req;
dat.post_Async_Action = action;
req.BeginGetResponse(new AsyncCallback(Post_Async_ResponesProceed), dat);
}

private static void Post_Async_ResponesProceed(IAsyncResult ar)
{
StreamReader reader = null;
Post_Async_Data dat = ar.AsyncState as Post_Async_Data;
HttpWebRequest req = dat.req;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
reader = new StreamReader(res.GetResponseStream());
string temp = reader.ReadToEnd();
res.Close();
req.Abort();
dat.post_Async_Action(temp);
}
}

 

所以总结下来基本上只要是懂得一点http协议,基本上网页版的程序,我们都可以自由的转换成图形桌面版,从而根据自己自身业务进行开发。

其次本人接触过 最多的就是淘宝客,淘宝天猫京东店数据,当然这些都是官方api的。kaola jd tianmao 等购物网站的分类,商品数据爬取分析(这些都是工作需要)。

第一次发新手教程很多可能看的不是很明白,不过有源码。可以根据源码的注释进行学习。其中最关键的是json的解析了。因为自己也是新手,所以很多都是要查一下资料才明白的。希望这几讲能给一些学习http协议的新手一些启示(我也是新手,说错勿怪。)

接下来就是做个图形界面的smart qq机器人。

 

转载于:.html

本文标签: 第六讲 smart qq C开发总结