C#中POST请求参数的问题

发布于 2024-11-15 08:37:40 字数 2763 浏览 2 评论 0原文

网站在用户点击日历更改日历时使用 POST 发送数据日期。我用 Firebug 来检查它。目标 URL 为 这个。特定示例的发布数据(以空格分隔)为 LeagueID=9 GameDate=4-29-2011 Season=2010-2011 Refresh=false LastUpdateTime=01-01-1900 type=Matchups RefreshStartTime=15-5- 2011-1308094688683周=conferenceID=

以下是标头:

Host    scores.covers.com
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Referer http://scores.covers.com/basketball-scores-matchups.aspx
Content-Length  169
Content-Type    text/plain; charset=UTF-8
Cookie  __loooooooooongCookieString

我想使用 WebRequest 发出 POST 请求(或其他任何方法)。这是我的尝试:

        string parameters = "LeagueID=\"9\"&GameDate=\"4-29-2011\"&Season=\"2010-2011\"&Refresh=\"false\"&LastUpdateTime=\"01-01-1900\"&type=\"Matchups\"&RefreshStartTime=\"15-5-2011-1308094688683\"&Week=&conferenceID=";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://scores.covers.com/ajax/SportsDirect.Controls.LiveScoresControls.Scoreboard,SportsDirect.Controls.LiveScoresControls.ashx?_method=UpdateScoreboard&_session=no");


        req.Method = "POST";
        req.ContentLength = bytes.Length;
        req.ContentType = "text/plain; charset=UTF-8";
        Console.WriteLine(req.ContentLength); // 175

        Stream reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();

        WebResponse resp = req.GetResponse();
        Console.WriteLine(((HttpWebResponse)resp).StatusDescription); // OK

        Stream respStream = resp.GetResponseStream();
        StreamReader reader = new StreamReader(respStream);
        Console.WriteLine(reader.ReadToEnd());
        resp.Close();

但它不起作用。响应代码没问题,但响应本身是这样的:

new Object();r.error = new ajax_error('System.FormatException','Input string was
 not in a correct format.\r\nCould not retreive parameters from HTTP request.',0
)new Object();r.error = new ajax_error('System.ArgumentException','Object of typ
e \'System.DBNull\' cannot be converted to type \'System.Int32\'.',0)

这是怎么回事?我可以看到参数有问题,因为请求的内容长度是 175(而不是 Firefox 发出的请求的 169)。

This website uses POST to send data whenever the user clicks on a calendar to change the date. I used Firebug to inspect it. The target URL is this. The post data (space-separated) for a particular example is LeagueID=9 GameDate=4-29-2011 Season=2010-2011 Refresh=false LastUpdateTime=01-01-1900 type=Matchups RefreshStartTime=15-5-2011-1308094688683 Week= conferenceID=.

And here are the headers:

Host    scores.covers.com
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Referer http://scores.covers.com/basketball-scores-matchups.aspx
Content-Length  169
Content-Type    text/plain; charset=UTF-8
Cookie  __loooooooooongCookieString

I'd like to make that POST request using WebRequest (or whetever else does the trick). Here's my attempt:

        string parameters = "LeagueID=\"9\"&GameDate=\"4-29-2011\"&Season=\"2010-2011\"&Refresh=\"false\"&LastUpdateTime=\"01-01-1900\"&type=\"Matchups\"&RefreshStartTime=\"15-5-2011-1308094688683\"&Week=&conferenceID=";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://scores.covers.com/ajax/SportsDirect.Controls.LiveScoresControls.Scoreboard,SportsDirect.Controls.LiveScoresControls.ashx?_method=UpdateScoreboard&_session=no");


        req.Method = "POST";
        req.ContentLength = bytes.Length;
        req.ContentType = "text/plain; charset=UTF-8";
        Console.WriteLine(req.ContentLength); // 175

        Stream reqStream = req.GetRequestStream();
        reqStream.Write(bytes, 0, bytes.Length);
        reqStream.Close();

        WebResponse resp = req.GetResponse();
        Console.WriteLine(((HttpWebResponse)resp).StatusDescription); // OK

        Stream respStream = resp.GetResponseStream();
        StreamReader reader = new StreamReader(respStream);
        Console.WriteLine(reader.ReadToEnd());
        resp.Close();

But it doesn't work. The response code is OK, but the response itself is this:

new Object();r.error = new ajax_error('System.FormatException','Input string was
 not in a correct format.\r\nCould not retreive parameters from HTTP request.',0
)new Object();r.error = new ajax_error('System.ArgumentException','Object of typ
e \'System.DBNull\' cannot be converted to type \'System.Int32\'.',0)

What's the deal? I can see that something's wrong with the params since the content length of the request is 175 (as opposed to the 169 from the request made by Firefox).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

獨角戲 2024-11-22 08:37:40

为什么不使用 NameValueCollection 来 POST 您的参数WebClient?它会为你做一些棘手的事情。链接页面底部的代码非常简单。与示例不同,您可能应该仔细处理 WebClient 的处理。

Why not use NameValueCollection to POST your parameters using a WebClient? It does the tricky stuff for you. The code at the bottom of the linked page is about as simple as it comes. Unlike the sample, you should probably deal thoughtfully with disposal of the WebClient.

楠木可依 2024-11-22 08:37:40

稍后指定 UTF-8 时不要进行 ASCII 编码。确保对参数进行 url 编码。尝试将内容类型更改为“x-www-form-urlencoded”。

Don't ASCII encode when you specify UTF-8 later. Make sure to url encode parameters. Try changing the content-type to 'x-www-form-urlencoded'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文