C#.net模拟提交表单POST

Posted on 2010-08-04 16:03  Love  阅读(291)  评论(0编辑  收藏  举报

方法一、
System.Net.WebClient WebClientObj         = new System.Net.WebClient();
    System.Collections.Specialized.NameValueCollection PostVars   = new System.Collections.Specialized.NameValueCollection();
    PostVars.Add("A1","0");
    PostVars.Add("A2","0");
    PostVars.Add("A3","000");

    try
    {
     byte[] byRemoteInfo    = WebClientObj.UploadValues("http://www.lovezhao.com/vote.asp","POST",PostVars);
     //下面都没用啦,就上面一句话就可以了
     string sRemoteInfo    = System.Text.Encoding.Default.GetString(byRemoteInfo);  
     //这是获取返回信息
     richTextBox_instr.Text   += sRemoteInfo;
    }
    catch
    {}



方法二、
string url = "网址";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string s = "要提交的数据";
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes (LoginInfo);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes,0,requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
string backstr = sr.ReadToEnd(); Response.Write(line); sr.Close(); res.Close();

 

Copyright © 2024 Love
Powered by .NET 9.0 on Kubernetes