利用WebClient实现自动添写、提交表单

1。要提交的表单是:http://stock.business.sohu.com/q/hp.php
表单要填写的内容保括:股票代码,时间范围
2。网页的源代码分析
这是我们要填写表单的内容:
 <form name=form_table method=post action=/q/hp.php OnSubmit="return checkvalue()">
 <td align=left valign=middle width=150 bgcolor=white>
  股票代码&nbsp;<INPUT size=6name=code value=600018>
  <input type=submit value='提交' id=submit name=submit>
  </td><td bgcolor=white valign=middle align=center width=250>
  开始日期&nbsp;<INPUT NAME=start_year VALUE=2004 SIZE=4 MAXLENGTH=4>&nbsp;年
  <SELECT NAME=start_month>
 <OPTION value=01>一月
<OPTION value=02>二月
<OPTION value=03>三月
<OPTION value=04>四月
<OPTION value=05>五月
<OPTION value=06 selected>六月
<OPTION value=07>七月
<OPTION value=08>八月
<OPTION value=09>九月
<OPTION value=10>十月
<OPTION value=11>十一月
<OPTION value=12>十二月
      </SELECT>&nbsp;月
  <INPUT NAME=start_day VALUE=28 SIZE=2 MAXLENGTH=2>&nbsp;日<br>
  结束日期&nbsp;<INPUT NAME=end_year VALUE=2005 SIZE=4 MAXLENGTH=4>&nbsp;年
  <SELECT NAME=end_month>
 <OPTION value=01>一月
<OPTION value=02>二月
<OPTION value=03>三月
<OPTION value=04>四月
<OPTION value=05>五月
<OPTION value=06>六月
<OPTION value=07 selected>七月
<OPTION value=08>八月
<OPTION value=09>九月
<OPTION value=10>十月
<OPTION value=11>十一月
<OPTION value=12>十二月
      </SELECT>&nbsp;月
  <INPUT NAME=end_day VALUE=28 SIZE=2 MAXLENGTH=2>&nbsp;日</td><td bgcolor=white valign=middle align=center>
 <input type=radio name=ss value=d  checked >交易日<br>
 <input type=radio name=ss value=w  >交易周<br>
 <input type=radio name=ss value=m  >交易月
 </td><td bgcolor=white valign=middle align=center>注: 所选记录请<br>不要超出300条。</td></tr>
  </FORM>

第一行告诉我们这个表单的action地址和发送方式,我们还可以得到股票代码,和日期的变量名称。下面我们就可以利用WebClient的UploadDate发送表单了。
public byte[] UploadData(
   string address,
   string method,
   byte[] data
);
代码如下:

 

             string  uriString  =   " http://stock.business.sohu.com/q/hp.php " ;
            
//  创建一个新的 WebClient 实例.
            WebClient myWebClient  =   new  WebClient();
            
string  postData  =   " code= " ;
            postData
+= textBox1.Text;
            postData
+= " &start_year= " ;
            postData
+= dateTimePicker1.Value.Year.ToString();
            postData
+= " &start_month= " ;
            
if (dateTimePicker1.Value.Month < 10 )
            
{
                
string str1="0"+dateTimePicker1.Value.Month.ToString();
                postData
+="0"+dateTimePicker1.Value.Month.ToString();
            }

            
else
            
{
                postData
+=dateTimePicker1.Value.Month.ToString();
            }

            postData
+= " &end_year= " ;
            postData
+= dateTimePicker2.Value.Year.ToString();
            postData
+= " &end_month= " ;
            
if (dateTimePicker2.Value.Month < 10 )
            
{
                postData
+="0"+dateTimePicker2.Value.Month.ToString();

            }

            
else
            
{
                postData
+=dateTimePicker2.Value.Month.ToString();
            }

            postData
+= " &start_day= " ;
            
if (dateTimePicker1.Value.Day < 10 )
            
{
                postData
+="0"+dateTimePicker1.Value.Day.ToString();
            }

            
else
            
{
                postData
+=dateTimePicker1.Value.Day.ToString();
            }

            postData
+= " &end_day= " ;
            
if (dateTimePicker2.Value.Day < 10 )
            
{
                postData
+="0"+dateTimePicker2.Value.Day.ToString();

            }

            
else
            
{
                postData
+=dateTimePicker2.Value.Day.ToString();
            }

            
//     02&end_year=2004&end_month=12&start_day=01&end_day=28";
            
//  注意这种拼字符串的ContentType
            myWebClient.Headers.Add( " Content-Type " , " application/x-www-form-urlencoded " );
            
//  转化成二进制数组
             byte [] byteArray  =  Encoding.ASCII.GetBytes(postData);
            
//  上传数据,并获取返回的二进制数据.
             byte [] responseArray  =  myWebClient.UploadData(uriString, " POST " ,byteArray);
            srcCode
= Encoding.ASCII.GetString(responseArray);
            
int  pos = 0 ;
            
if ((pos = srcCode.IndexOf( " <br><br><br><center> " )) !=- 1 )
            
{
                MessageBox.Show(
"您输入的股票代码不存在");
                
return;

            }

            AnsCode(srcCode);
srcCode是获得提交后的网页源代码,AnsCode是处理这些代码,以提取股票的各种数据放入数据库

你可能感兴趣的:(client)