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

C# httpwebrequest批量提交数据到网站。

时间:2015-05-14 13:52:55      阅读:677      评论:0      收藏:0      [点我收藏+]

标签:

非新文,由自己以前在baidu网站转来的。(现在百度的博客不喜欢,转来这存档。很久以前的工作笔记了)

因为工作需要要将大致300多条数据提交到网站,而网站在提交 错数据后无法正常进入相应的数据页面进行修改,

不得己自己动手开发一个。

关键字:httpwebrequest, httpwebrespone, excel ,datagradview,httpwatch.cookie

首先下载一个httpwatch安装,在IE中截获提交信息

登录流:

POST /index.php/Public/checkLogin/ HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: http://syd.fjedu.gov.cn/index.php/Public/login
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Host: syd.fjedu.gov.cn
Content-Length: 101
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: PHPSESSID=22g4terki0cnj498q17duhim46

account=账号名的URL编码(具体要编码后才知道)&password=XXXXXXXX&verify=8251 

红色因保密需要而修改。

提交流:

POST /index.php/Sydmd/update/navTabId/index HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: zh-cn
Referer: http://syd.fjedu.gov.cn/index.php/Index/index#index
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)
Host: syd.fjedu.gov.cn
Content-Length: 270
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: PHPSESSID=22g4terki0cnj498q17duhim46; _currentUrl_=czoyMjoiL2luZGV4LnBocC9TeWRtZC9pbmRleCI7

id=数据ID&name=姓名sex=性别&sfzid=身份证&minzhu=民族cityid=58&xianid=573&jhr_name=%E6%9E%97%E6%9C%88%E5%89%8D&jhr_tel=13799492115&address=%E5%BE%B7%E5%8C%96%E5%8E%BF%E6%9D%A8%E6%A2%85&knlx=2&ndkje=6000.00&mdremark=

红色部分保密,也为输入的参数。id为己提交数据时产生的ID,要在网站复制下来才可以得到。

二、登录

1cookie取得。为了取得cookie先get主页面

此代码在form1中执行,即程序运行即自动获取。

 HttpWebRequest req;
            WebResponse wrp;
            Cookies = new CookieContainer();
           // Cookies.SetCookies("http://syd.fjedu.gov.cn/index.php/Public/login", "PHPSESSID=22g4terki0cnj498q17duhim46");
            InitializeComponent();
            req = WebRequest.Create(Index_URL) as HttpWebRequest;
            //req.Method = "GET";
            req.ProtocolVersion = HttpVersion.Version11;
           // req.Headers.Add("1.1"); 
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            //req.CookieContainer=Cookies ;
 
            //在这里对接收到的页面内容进行处理
            try
            {
                HttpWebResponse hwrs = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(hwrs.GetResponseStream());
                String message = reader.ReadToEnd();
                RTB.Text = message;
                strCookie = hwrs.GetResponseHeader("Set-Cookie");//这里的strcookie是字符,不是C#中的Cookie类型,所以后面的使用以真正的cookie类型不同
                req.Abort(); 

            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (req != null)
                    req.Abort();
            }

2.获取验证码图片并显示。

private void pictureBox1_Click(object sender, EventArgs e)
        {
            HttpWebResponse wresponse;
            Stream s;
            HttpWebRequest req;
            req = (HttpWebRequest)HttpWebRequest.Create(Image_URL);
            req.Headers.Add("Accept-Language", "zh-cn");
            req.Headers.Add("Cookie", strCookie);//加入cookie才有效,没有cookie则取得的无效。。
            wresponse = (HttpWebResponse)req.GetResponse();
            try
            {

                s = wresponse.GetResponseStream();
                System.Drawing.Image simg = System.Drawing.Image.FromStream(s);
                Bitmap newimg = new Bitmap(simg);
                pictureBox1.Image = simg;
                //Cookies = req.CookieContainer;
                //wresponse.Close;
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (req != null)
                    req.Abort();
            }

           //pictureBox1.Load("http://syd.fjedu.gov.cn/index.php/Public/verify/");这个代码无cookie无效,所以不用。虽可显示。
        }

3.登录代码:

 private void button1_Click(object sender, EventArgs e)
        {
            //登录
            //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
            HttpWebRequest req1;
            string postData =String.Format("account={0}&password={1}&verify={2}",textBox1.Text , textBox2.Text, textBox3.Text);

            req1 = WebRequest.Create(LOGIN_URL) as HttpWebRequest;
            try
            {


                //StreamWriter requestWriter1 = new StreamWriter(req1.GetRequestStream());
                req1.Method = "POST";

                req1.ContentType = "application/x-www-form-urlencoded";
                req1.ProtocolVersion = HttpVersion.Version11;
                req1.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
                //Cookie: PHPSESSID=22g4terki0cnj498q17duhim46
                req1.Headers.Add("Accept-Language", "zh-cn");
                req1.Headers.Add("Accept-Encoding", "gzip, deflate");
                //req.Connection = Keep - Alive;
                req1.KeepAlive = true;
                //req.RequestUri = "/index.php/Public/checkLogin/";
                req1.Headers.Add("Cookie", strCookie);//加入cookie才有效,没有cookie则取得的无效。。

                
                Encoding encoding = Encoding.GetEncoding("utf-8");
                
                //UrlEncode(postData, encoding);
                byte[] bytesToPost = encoding.GetBytes(postData);
                req1.ContentLength = bytesToPost.Length;

                System.IO.Stream requestStream = req1.GetRequestStream();
                requestStream.Write(bytesToPost, 0, bytesToPost.Length);
                requestStream.Close();
            

                HttpWebResponse hwrs1 = (HttpWebResponse)req1.GetResponse();
                StreamReader reader1 = new StreamReader(hwrs1.GetResponseStream());
                String message1 = reader1.ReadToEnd();
                RTB.Text = message1;
                //requestWriter.Close();成功....

            }
            catch (WebException ex)
            {
                throw ex;
            }
                finally
                {
                    if (req1 != null)
                        req1.Abort();
                }

 }

4.读取excel并填 充datagridview.

须引用:using System.Data.OleDb;

            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:/data.xls;" +
    "Extended Properties=‘Excel 8.0;HDR=Yes;IMEX=1‘";
            DataSet ds = new DataSet();
            OleDbDataAdapter oada = new OleDbDataAdapter("select * from [index$]", strConn);
            
            oada.Fill(ds);
            try
            {
                dataGridView1.DataSource = ds.Tables[0];//读取第一个表格,以excel表的顺序为准。具体可以自己试。
            }
            catch (Exception ex)
            {
                throw ex;
            }
      5.批量提交:

     

 string Sid, sName,ssex,ssfzid,sminzhu,scityid,sxianid,sjhr_name,sjhr_tel,saddress,sknlx ;//提交顺序处理表格,并定义。
            //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
            HttpWebRequest req2;
            //sName,ssex,ssfzid,sminzhu,scityid,sxianid,sjhr_name,sjhr_tel,saddress,sknlx

            string postData = "";
            int i = 0,j=0;
            for (j = 0; j < 302; j++)
            {
                //sName = dataGridView1[i, j].Value.ToString(); i++;

                         req2 = WebRequest.Create(Post_URL) as HttpWebRequest;//httpwebrequest赋值后不能修改,所以放在循环中生成
                try
                {


                    //StreamWriter requestWriter1 = new StreamWriter(req1.GetRequestStream());
                    req2.Method = "POST";

                    req2.ContentType = "application/x-www-form-urlencoded";
                    req2.ProtocolVersion = HttpVersion.Version11;
                    req2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
                    //Cookie: PHPSESSID=22g4terki0cnj498q17duhim46
                    req2.Headers.Add("Accept-Language", "zh-cn");
                    req2.Headers.Add("Accept-Encoding", "gzip, deflate");
                    //req.Connection = Keep - Alive;
                    req2.KeepAlive = true;
                    //req.RequestUri = "/index.php/Public/checkLogin/";
                    req2.Headers.Add("Cookie", strCookie);


                    //id Sname Xb sfzid jhr_name jhr_tel address knlx minzhu cityid xianid
                    i = 0;
                    Sid = dataGridView1[i, j].Value.ToString(); i++;
                    sName = dataGridView1[i, j].Value.ToString(); i++;
                    ssex = dataGridView1[i, j].Value.ToString(); i++;
                    ssfzid = dataGridView1[i, j].Value.ToString(); i++;
                    sjhr_name = dataGridView1[i, j].Value.ToString(); i++;
                    sjhr_tel = dataGridView1[i, j].Value.ToString(); i++;
                    saddress = dataGridView1[i, j].Value.ToString(); i++;
                    sknlx = dataGridView1[i, j].Value.ToString(); i++;
                    sminzhu = dataGridView1[i, j].Value.ToString(); i++;
                    scityid = dataGridView1[i, j].Value.ToString(); i++;
                    sxianid = dataGridView1[i, j].Value.ToString();
                    postData = String.Format("id={0}&name={1}&sex={2}&sfzid={3}&minzhu={4}&cityid={5}&xianid={6}&jhr_name={7}&jhr_tel={8}&address={9}&knlx={10}&ndkje=6000.00&mdremark=", Sid, sName, ssex, ssfzid, sminzhu, scityid, sxianid, sjhr_name, sjhr_tel, saddress, sknlx);//中间的&号不要掉了,会出错,却记
                    Encoding encoding = Encoding.GetEncoding("utf-8");
                    byte[] bytesToPost = encoding.GetBytes(postData);


                    req2.ContentLength = bytesToPost.Length;

                    System.IO.Stream requestStream = req2.GetRequestStream();
                    requestStream.Write(bytesToPost, 0, bytesToPost.Length);

 

                    HttpWebResponse hwrs2 = (HttpWebResponse)req2.GetResponse();
                    StreamReader reader2 = new StreamReader(hwrs2.GetResponseStream());
                    String message2 = reader2.ReadToEnd();
                    RTB.Text = message2;//下面最好有结果判断。。。
                    requestStream.Close();
                   // progressBar1.Increment(1);
                    progressBar1.Value = j;
                    
                    
                    //requestWriter.Close();鎴愬姛....

                }
                catch (WebException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (req2 != null)
                        req2.Abort();//关闭httpwebrequest以便再生成。。。。
                }
            }//j循环结束

        }
三小结。

此程序历时一天半才完成,期间遇到问题有:

1.httpwebrequest的使用问题,不可重复使用真让人郁闷。。

2.excel导入到datagridview

3..httpwebrequest使用cookie也是很大的问题。。。

C# httpwebrequest批量提交数据到网站。

标签:

原文地址:http://www.cnblogs.com/CatDo/p/4502875.html

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