简体   繁体   English

如何以编程方式将GET / POST请求发送到简单的ASPX页面?

[英]How to send GET/POST request programmatically to simple ASPX page?

I use following code to post querystring 我使用以下代码发布querystring

string URI = "http://somewebsite.com/default.aspx";
string myParameters = "param1=value1&param2=value2&param3=value3";
using (WebClient wc = new WebClient())
{
   wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
   string HtmlResult = wc.UploadString(URI, myParameters);
}

But somehow default.aspx does not accept that post call. 但是,以某种方式default.aspx不接受该调用。

The point is when I manually in browser go to http://somewebsite.com/default.aspx all code there is working fine. 关键是当我在浏览器中手动转到http://somewebsite.com/default.aspx所有代码都可以正常工作。

My questions is following what do I am missing here to archive the same result when I open page manually as I do it with WebClient ? 我的问题是,在我手动打开页面时(与使用WebClient一样)时,我在这里缺少什么来归档相同的结果?

Thank you in advance! 先感谢您!

PS 1 PS 1

I just tried to use GET method to that URL and it has no effect also. 我只是尝试对该URL使用GET方法,它也没有任何作用。 How is it possible? 这怎么可能? What is difference between manual navigation to page and sending GET/POST? 手动导航到页面与发送GET / POST有什么区别?

PS 2 PS 2

I even tried this 我什至尝试过

wc.Headers["Accept"] = "application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";

and and Load event of Default.aspx is not hiting. 并且没有击中Default.aspx的 Load事件。 :( :(

From your description of what you want to achieve, I think you may have chosen the wrong WebClient method. 从您对要实现的目标的描述中,我认为您可能选择了错误的WebClient方法。 Instead of UploadString , try DownloadString : 而不是UploadString ,请尝试DownloadString

using (WebClient wc = new WebClient())
{
  string HtmlResult = wc.DownloadString("http://somewebsite.com/default.aspx?param1=value1&param2=value2&param3=value3");
}

So that comment is correct one 所以那个评论是正确的

"What is difference between manual navigation to page and sending GET/POST?" “手动导航到页面与发送GET / POST有什么区别?” - see for yourself, for example using Fiddler. -自己看看,例如使用Fiddler。 – CodeCaster – CodeCaster

I checked all requests with Fiddler and found that there is code of base page class that redirects to Index page. 我检查了所有与Fiddler的请求,发现有一些基页类的代码重定向到索引页。 So Load event is never happened. 因此,Load事件永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM