码迷,mamicode.com
首页 > Windows程序 > 详细

c# post 数据的方法

时间:2015-05-06 16:51:39      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

网页自动登录和提交POST信息的核心就是分析网页的源代码(HTML),在C#中,可以用来提取网页HTML的组件比较多,常用的用WebBrowser、WebClient、HttpWebRequest这三个。

以下就分别用这三种方法来实现:
1、WebBrowser是个"迷你"浏览器,其特点是Post时不用关心Cookie、内置JS等问题
WebBrowser是VS2005新提供的组件(其实就是封装了IE接口),实现POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 来实现,代码如下:

        

   
技术分享
技术分享
HtmlElement ClickBtn =null;
           if (e.Url.ToString().ToLower().IndexOf("http://hi.baidu.com/srxljl"> 0)   //登陆页面
            {
                HtmlDocument doc = webBrowser1.Document;
                for (int i = 0; i < doc.All.Count ; i++)
                {
                    if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                    {
                        switch (doc.All[i].Name)
                        {
                            case "userCtl":
                                doc.All[i].InnerText = "user01";
                                break;
                            case "passCt1":
                                doc.All[i].InnerText = "mypass";
                                break;
                            case "B1":
                                ClickBtn = doc.All[i]; //提交按钮
                                break;
                        }
                    }
                }
                ClickBtn.InvokeMember("Click");   //执行按扭操作
            }
技术分享

 

2、WebClient封装了HTTP的一些类,操作简单,相较于webBrowser,特点是可以自设代理,缺点是对COOKIE的控制
WebClient的运行全在后台,并且提供了异步操作的能力,这样很方便并发多个任务,然后等待结果的返回,再逐个处理。多任务异步调用的代码如下:

   

技术分享
技术分享
private void StartLoop(int ProxyNum)
        {
           WebClient [] wcArray = new WebClient[ProxyNum]; //初始化
             for (int idArray = 0; idArray< ProxyNum;idArray++)
            {
                 wcArray[idArray] = new WebClient();
                wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
                wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
                try
                {
                   
                    wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
                    wcArray[idArray].OpenReadAsync(new Uri("http://hi.baidu.com/srxljl")); //打开WEB;
                    proxy = null;
                }
                catch
                {
                }
            }
        }

        private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs e)
        {
                if (e.Error == null)
                {
                            string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //取返回信息
                             ..
                              String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"];
                             ((WebClient)sender).Headers.Add("Content-Type""application/x-www-form-urlencoded");
                            ((WebClient)sender).Headers.Add("Accept-Language""zh-cn");
                            ((WebClient)sender).Headers.Add("Cookie", cookie);

                            string postData = ""
                            byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成二进制数组
                           ((WebClient)sender).UploadDataAsync(new Uri("http://hi.baidu.com/srxljl"), "POST", byteArray);
                }
         }

        private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
        {
                 if (e.Error == null)
                {
                    string returnMessage = Encoding.Default.GetString(e.Result);
                   
                }
       }
技术分享

3、HttpWebRequest较为低层,能实现的功能较多,Cookie操作也很简单:


      

技术分享
技术分享
  private string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
        {
           string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ret;
        }
技术分享

 

HttpWebRequest 实现, 做了一个简单测试程序,在最近的项目中写了一个调用第三方接口短信收发功能,也就是通过网站向手机发送信功能,同时在第三方文档资料只给出了URL、参数列表及返回值.通过C#的HttpWebRequest类很容易实现这个功能。

这个程序刚好用上,用来当做测试工具使用。

这个测试程序,做成了动态参数形式,即可以根据不同的服务方设置不同的URL、参数。但前提都得支持通过POST方式服务。

当然也有一些服务接口是通过构造XML文件,然后提交XML文件服务形式。

要加上支持构造XML格式也很简单,形式大同小异,这里暂不讨论,通过构建XML文件然后POST文章请看此处post提交XML

 

技术分享

 

添加参数及对应值,敏感信息可以通过加密后再发送。加密算法是调用系统MD5算法。目前来说MD5算法还是比较安全的。

例如:

第三方提供的服务接口类似: http://www.cnblogs.com/service?user=xxx&pass=yyy 

要实现调用服务提交post数据(注:这里并不是真正服务接口,只是举个说明).

1.在测试工具只要填写

服务URL:   http://www.cnblogs.com/serveice

2.设置参数列表

参数名称:user,值:xxx;

参数名称:pass,值:yyy;

......

3.提交就行了,可能不同的服务返回信息不同,这里只做了返回字符串形式。

 

技术分享

 

用HttpWebRequest实现起来还是比较简单方便,这种方式在webservice中广泛应用。

另外在自动登录中可以实现类似做法,但是验证码目前暂没有解决,但也可以通过httpWebRequest去请求实现。

请求获取图片方法:

HttpWebResponse resp;
技术分享        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.csdn.net/Images/logo_csdn.gif");
技术分享        req.Timeout = 150000;
技术分享        resp = (HttpWebResponse)req.GetResponse();
技术分享        System.Drawing.Image img;
技术分享        img = new System.Drawing.Bitmap(resp.GetResponseStream());
技术分享

HttpWebRequest自动登录网站并获取网站内容(不包含验证码的网站)
可以使用 Visual Sniffer(百度搜索) 来捕捉提交的数据信息:
1. 访问你需要站外提交的页面,比如 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx
2. 填写好需要的资料,比如用户名和密码,
3. 打开 Visual Sniffer, 点“开始拦截”
4. 在访问的页面中提交。
5. 等提交成功之后,在 Visual Sniffer 中“停止拦截”
6. 在 Visual Sniffer 的左侧栏的加号中依次点开,

右边是它拦截到的内容:


POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, **
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net

c# post 数据的方法

标签:

原文地址:http://www.cnblogs.com/tiancai/p/4482204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!