我尝试使用WebClient类在POST请求的正文中传递一个字符串。以下是我的代码:
protected void GetAccessToken(string client_id, string secret)
{
var urlEncodedSecret = HttpUtility.UrlEncode(secret);
var urlEncodedSid = HttpUtility.UrlEncode(client_id);
string bodyData = String.Format("grant_type=client_credentials&client_id=ms-app%3a%2f%2f{0}&client_secret={1}&scope=notify.windows.com", client_id, secret);
string address = "https://login.live.com/accesstoken.srf";
string myParameters = "param1=value1¶m2=value2¶m3=value3";
Uri URI = new Uri(address);
System.Net.WebClient webClient = new WebClient();
webClient.BaseAddress = address;
webClient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
webClient.Headers["Content-Length"] = "211";
webClient.Headers["Host"] = "https://login.live.com";
webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(uploadStringCompleted);
webClient.UploadStringAsync(URI, "POST", bodyData);
}
private void uploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
string json = e.Result.ToString();
_oAuthToken = GetOAuthTokenFromJson(json);
}
当我运行应用程序时,我收到异常"远程服务器返回错误:NotFound。"。 我使用邮递员成功地提出了同样的要求:
任何人都知道我的要求有问题吗?或者是让我走上正确道路的东西!?