C# HttpWebRequest如何使用Post提交网页?

uestczcs 2010-10-07 05:42:19
请教各位高手,这个网页
http://www.taifex.com.tw/chinese/3/3_1_1.asp?syear=2010&smonth=10&sday=7
如何通过Post的方法修改契约,然后提交,谢谢!
我修改了契约但是得到的页面还是旧的,不知道什么原因。

代码:

String strUrl = @"http://www.taifex.com.tw/chinese/3/3_1_1.asp?syear=2010&smonth=10&sday=7";

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);

string PostData = @"commodity_idt=CPF";
WriteLogFile(PostData);
byte[] bs = Encoding.ASCII.GetBytes(PostData);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

Stream outStream = req.GetRequestStream();
outStream.Write(bs,0,bs.Length);
outStream.flush();
outStream.Close();

WebResponse myResp = null;
try { //接收HTTP做出的响应
myResp = req.GetResponse();
}
catch(Exception e)
{
int ii =0;
}
Stream ReceiveStream = myResp.GetResponseStream();

StreamReader sReader = new StreamReader(ReceiveStream);
string strContent = sReader.ReadToEnd();

ReceiveStream.Close();
sReader.Close();
myResp.Close();
...全文
369 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
uestczcs 2010-10-08
  • 打赏
  • 举报
回复
谢谢楼上,半夜还在辛勤的工作,呵呵!
uestczcs 2010-10-08
  • 打赏
  • 举报
回复
谢谢楼上,果然是参数不全!
jshi123 2010-10-08
  • 打赏
  • 举报
回复
你漏掉了一些参数,补上就可以了

String strUrl = @"http://www.taifex.com.tw/chinese/3/3_1_1.asp?syear=2010&smonth=10&sday=7";

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strUrl);

string PostData = @"commodity_idt=CPF" +
"&qtype=2" +
"&commodity_id=CPF" +
"&commodity_id2=" +
"&syear=2010" +
"&smonth=10" +
"&sday=7";


WriteLogFile(PostData);
byte[] bs = Encoding.ASCII.GetBytes(PostData);

req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

Stream outStream = req.GetRequestStream();
outStream.Write(bs,0,bs.Length);
outStream.flush();
outStream.Close();

WebResponse myResp = null;
try { //接收HTTP做出的响应
myResp = req.GetResponse();
}
catch(Exception e)
{
int ii =0;
}
Stream ReceiveStream = myResp.GetResponseStream();

// 注意要用繁体编码接收数据
StreamReader sReader = new StreamReader(ReceiveStream, Encoding.GetEncoding("big5"));
string strContent = sReader.ReadToEnd();

ReceiveStream.Close();
sReader.Close();
myResp.Close();
uestczcs 2010-10-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sp1234 的回复:]
搞清楚你的PostData是否只需要这一点东西就足够?!
[/Quote]

我不是很熟悉网页, 我只用 string PostData = @"commodity_idt=CPF";是因为在
webbrowser1里面只设置commodity_idt的值,然后invokescript就可以,我最大的疑惑其实
是使用post的时候怎么提交,是自动提交的吗?
uestczcs 2010-10-07
  • 打赏
  • 举报
回复
webbrowser1我用了,很简单方便,但我是想了解下Post的用法,因为有时我会写控制台程序!
wuyq11 2010-10-07
  • 打赏
  • 举报
回复
string param = "syear=2010&smonth=10&sday=7";
byte[] bs = Encoding.ASCII.GetBytes(param);
HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create( "" );
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

new System.Net.WebClient().DownloadString("");

webbrower试试
  • 打赏
  • 举报
回复
搞清楚你的PostData是否只需要这一点东西就足够?!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧