Wordnik身份验证错误

时间:2012-03-15 13:16:49

标签: c# authentication

我正在尝试使用提供的API从Wordnik检索身份验证令牌。但是,我似乎无法让它工作;我似乎陷入了401和403错误。

以下是我用于从API请求身份验证的代码:

string authRequest = 
    String.Format("http://api.wordnik.com//v4/account.json/authenticate/{0}",
    this.userName);

HttpWebRequest request = WebRequest.Create(authRequest) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";

// When this is added, I get 403 errors
///request.Headers.Add("api_key", APIKey);

string postData = "password=" + password;
byte[] encodedData = UTF8Encoding.UTF8.GetBytes(postData);
request.ContentLength = encodedData.Length;

Stream stream = request.GetRequestStream();
stream.Write(encodedData, 0, encodedData.Length);
stream.Close();

string responseText;

using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    using(StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        responseText = reader.ReadToEnd();
        Console.WriteLine(responseText);
        Console.ReadLine();
    }
}

你们有谁能告诉我我做错了什么吗?

赞赏任何意见。

1 个答案:

答案 0 :(得分:0)

请求网址中有一个双斜杠:

http://api.wordnik.com//v4