c# Winfrom通过HttpWebRequest 发送Http请求Post方法举例(登录功能)

Winfrom通过HttpWebRequest 发送Http请求Post、Get方法举例

C#篇章

添加引用 using System.Net; using System.Web;

代码一(Post方法举例)

			//登陆时以模拟提交表单请求
				ASCIIEncoding encoding = new ASCIIEncoding();
              string postData = "username=" + strId;
              postData += ("&password=" + strPassword);
              byte[] data = encoding.GetBytes(postData);
              //创建一个HTTP请求
              HttpWebRequest request =
              (HttpWebRequest)WebRequest.Create(url);
              request.Method = "POST";
              request.ContentType = "application/x-www-form-urlencoded";
              request.ContentLength = data.Length;
              Stream newStream = request.GetRequestStream();
              // 发送数据
              newStream.Write(data, 0, data.Length);
              newStream.Close();
             // 接收后台返回的数据
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
              string content = reader.ReadToEnd();
              Console.WriteLine(content);

代码二(获得上面Post请求的Cookie值方便在get方式时调用)

关于Cookie以及Session的概念、自行百度

			CookieContainer cc=request.CookieContainer
		 public static string GetAllCookies(CookieContainer Cookie)
          {
              string cookieValue = "";
              Hashtable table = (Hashtable)cc.GetType().InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cc, new object[] { });
              foreach (object pathList in table.Values)
              {
                  SortedList lstCookieCol = (SortedList)pathList.GetType().InvokeMember("m_list", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, pathList, new object[] { });
                  foreach (CookieCollection colCookies in lstCookieCol.Values)
                      foreach (Cookie c in colCookies)
                      {
                          Console.WriteLine(c.Domain + ":" + c.Name + "____" + c.Value + "\r\n");
                          Console.WriteLine(c.Value);
                          cookieValue = c.Value;
                      }
              }
              return cookieValue;
          }

代码三(get方法举例)

					
			 //登录后的Cookie值
			 string cookieStr = "shiroCookie="+GetAllCookies(request.CookieContainer);
			 //创建一个Http请求
              HttpWebRequest request =
              (HttpWebRequest)WebRequest.Create(url);
              //将Cookie值添加到请求头中
              request.Headers.Add("Cookie", cookieStr);  
              request.Method = "GET";
              request.ContentType = "application/x-www-form-urlencoded";
              HttpWebResponse myResponse = (HttpWebResponse)request.GetResponse();
              //以UTF-8的编码格式接收返回的数据
              StreamReader reader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
              string content = reader.ReadToEnd();
              Console.WriteLine(content);
              MessageBox.Show(content);
以上是我对C#winfrom技术实现Http请求的个人见解、大家可以参考、有问题的可以留言一起讨论
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值