/// <summary> /// 发送POST请求到HTTP站点 /// </summary> public static void Test3() { string loginUrl = "http://home.51cto.com/index.php?s=/Index/doLogin"; string userName = "******"; string password = "******"; IDictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("email", userName); parameters.Add("passwd", password); HttpWebResponse response = HttpWebReqUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, Encoding.UTF8, null); Stream myResponseStream = response.GetResponseStream(); if (myResponseStream != null) { var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); Console.WriteLine(retString); } Console.WriteLine("null"); }
/// <summary> /// POST数据到HTTPS站点 /// </summary> public static void Test1() { string loginUrl = "https://passport.baidu.com/?login"; string userName = "******"; string password = "******"; string tagUrl = "http://cang.baidu.com/" + userName + "/tags"; Encoding encoding = Encoding.GetEncoding("gb2312"); IDictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("tpl", "fa"); parameters.Add("tpl_reg", "fa"); parameters.Add("u", tagUrl); parameters.Add("psp_tt", "0"); parameters.Add("username", userName); parameters.Add("password", password); parameters.Add("mem_pass", "1"); HttpWebResponse response = HttpWebReqUtility.CreatePostHttpResponse(loginUrl, parameters, null, null, encoding, null); string cookieString = response.Headers["Set-Cookie"]; // response.ResponseUri Stream myResponseStream = response.GetResponseStream(); if (myResponseStream != null) { var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); Console.WriteLine(retString); } Console.WriteLine("null"); }
/// <summary> /// 发送GET请求到HTTP站点 /// </summary> public static void Test2() { string userName = "******"; string tagUrl = "http://cang.baidu.com/" + userName + "/tags"; var cookies = new CookieCollection(); //如何从response.Headers["Set-Cookie"];中获取并设置CookieCollection的代码略 HttpWebResponse response = HttpWebReqUtility.CreateGetHttpResponse(tagUrl, null, null, cookies); Stream myResponseStream = response.GetResponseStream(); if (myResponseStream != null) { var myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); Console.WriteLine(retString); } Console.WriteLine("null"); }