0
尝试发送请求,但它并不适用于某些原因:在C#中cURL模拟?
这是应该curl命令
curl --data "client_id={client_id}&client_secret={client_secret}&code={code}&grant_type=authorization_code&redirect_uri={redirect_uri}" https://cloud.testtest.com/oauth/access_token.php
但在C#我已经建立了工作这一个:
var webRequest = (HttpWebRequest)WebRequest.Create("https://cloud.merchantos.com/oauth/access_token.php");
webRequest.Method = "POST";
if (requestBody != null)
{
webRequest.ContentType = "application/x-www-form-urlencoded";
using (var writer = new StreamWriter(webRequest.GetRequestStream()))
{
writer.Write("client_id=1&client_secret=111&code=MY_CODE&grant_type=authorization_code&redirect_uri=app.testtest.com");
}
}
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException exception)
{
var responseStream = exception.Response.GetResponseStream();
if (responseStream != null)
{
var reader = new StreamReader(responseStream);
string text = reader.ReadToEnd().Trim();
throw new WebException(text);
}
}
请指教。由于某种原因不能确定为什么代码不起作用
请说明“不行”。 – Dmitry 2014-09-01 09:43:32
我想我编写的代码不起作用,因为我从服务器接收到400个错误的请求。听起来像我在C#代码中犯了一个错误 – Sergey 2014-09-01 09:46:48
你需要从服务器读取响应,它应该给你一个Json格式的错误描述。如果我使用你指定的细节(显然你已经改变了证书),它会给出HTTP 400错误:'error_description =客户证书无效' – DavidG 2014-09-01 09:54:53