后台模拟一个http请求如何做?

一个臭皮匠er 2011-01-17 03:01:32
比如想在一个button事件中,自己定义一个html字符串发出post或get的http请求,另一个页面request接收,就和html submit的一样,如何做?用httpwebrequest+httpwebresponse? 能给个例子吗
...全文
226 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
一个臭皮匠er 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]

HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)
{
byte[] bdata = Encoding.Default.GetBytes(po……
[/Quote]
但是如何设置参数名了。比如人家提供的接口是request["name"]该如何办呢
jxc521523 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)
{
byte[] bdata = Encoding.Default.GetBytes(pos……
[/Quote]
++
jxc521523 2011-01-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)
{
byte[] bdata = Encoding.Default.GetBytes(pos……
[/Quote]
++
wuyq11 2011-01-17
  • 打赏
  • 举报
回复
HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
private string HttpWebRequestLogin(string loginUrl, string postData, ref CookieContainer cookieContainer)
{
byte[] bdata = Encoding.Default.GetBytes(postData);
System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(loginUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
myRequest.Referer = loginUrl;
myRequest.KeepAlive = true;
myRequest.AllowAutoRedirect = true;
if (cookieContainer == null)
cookieContainer = new CookieContainer();
myRequest.CookieContainer = cookieContainer;
myRequest.ContentLength = bdata.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(l_data, 0, bdata.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string content = reader.ReadToEnd();
return content;
}


wuyq11 2011-01-17
  • 打赏
  • 举报
回复
HttpWebRequest通过post等提交到相关页面,获取执行的页面数据
byte[] bdata = Encoding.Default.GetBytes(postData);
System.Net.HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = bdata.Length;
Stream newStream = myRequest.GetRequestStream();

62,017

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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