smart qqC#開發總結: 整個開發下來其實一點都不是很難,從一開始二維碼 獲取到最終的收發消息,基本上都是模擬瀏覽器的操作。都是基於http通訊。一下就是 本次新手學習http協議的最關鍵的一個類 /// <summary> /// HTTP網路通信類 /// </summary> publi ...
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 = "http://d1.web2.qq.com/proxy.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 = "http://d1.web2.qq.com/proxy.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 = "http://d1.web2.qq.com/proxy.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機器人。