C#.net模拟提交表单POST(转载)

本文介绍了两种使用C#.NET模拟POST请求的方法:一种是利用System.Net.WebClient上传表单数据;另一种是通过WebRequest创建POST请求并发送自定义的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法一、
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); //这里的编码方式可能不一样
//这是获取返回信息
    Console.WriteLine(sRemoteInfo );
}
catch
{}



方法二、

优快云上面的

public static void Test ()
{
WebRequest request = WebRequest.Create("http://soso.music.qq.com/fcgi-bin/fcg_song.fcg");
request.Method = "POST";
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.GetEncoding("gb2312").GetBytes(postData);
//Encoding.UTF8.GetBytes (postData);//你想要的编码方式
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("gb2312"));//这里也要编码.根据你的需要
string responseFromServer = reader.ReadToEnd ();
Console.WriteLine (responseFromServer);
reader.Close ();
dataStream.Close ();
response.Close ();
}

 

可能得到的会乱码,你要自己去看看原来那个网站上的使用的是上面编码

不太好直接 转载,我就只要 把原文 复制下来了.出处见 http://www.blogjava.net/wangxinsh55/archive/2007/06/22/125847.html

转载于:https://www.cnblogs.com/limiao/archive/2012/12/28/2837743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值