WebClient,不发送参数

时间:2011-12-06 14:50:47

标签: c# fogbugz-api

我尝试从我的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日志中看到请求,但参数不存在。

我做错了什么?

编辑:我确定参数是正确的,因为我测试了在浏览器中它运行良好。

1 个答案:

答案 0 :(得分:1)

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

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

相反,我认为您应该根据documentation在查询字符串中发送此信息(请参阅“登录”部分):

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

如果您想使用NameValueCollection来构建查询字符串,则此answer提供了一种方法。