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