WebClient,不发送参数

本文关键字:参数 WebClient | 更新日期: 2023-09-27 18:16:51

我尝试从我的fogbugz网站获取令牌,如下:

http://fogbugz.stackexchange.com/fogbugz-xml-api

我有:

using (var wb = new WebClient())
{   
                var data = new NameValueCollection();
                data["cmd"] = HttpUtility.UrlEncode(cmdLogon);
                data["email"] = HttpUtility.UrlEncode(email);
                data["password"] = HttpUtility.UrlEncode(password);
                content = encoding.GetString(wb.UploadValues(url, "POST", data));
 }

服务器响应下面:

<?xml version="1.0" encoding="UTF-8"?><response><error code="1">Nom d'utilisateur ou mot de passe incorrect</error></response>

我可以在IIS日志中看到请求,但是没有参数。

我做错了什么?

我确信参数是正确的,因为我在浏览器中测试过了,它工作得很好。

WebClient,不发送参数

我用Fiddler运行了这个,你的代码发送了一个请求,请求体中有以下内容:

cmd=logon&email=test-email&password=test-password

相反,我认为您应该按照文档(请参阅"登录"部分)在查询字符串中发送此信息:

http://www.example.com/api.asp?cmd=logon&email=xxx@example.com&password=BigMac

如果您想使用NameValueCollection来构建您的查询字符串,这个答案提供了一种方法。