我正在使用 API 并尝试在 C# 中执行 JSON PUT 请求。这是我正在使用的代码:
public static bool SendAnSMSMessage()
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apiURL");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = **// Need to put data here to pass to the API.**
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
//Now you have your response.
//or false depending on information in the response
return true;
}
}
问题是我不知道如何将数据传递给 API。所以就像在 JavaScript 中我会做这样的事情来传递数据:
type: 'PUT',
data: { 'reg_FirstName': 'Bob',
'reg_LastName': 'The Guy',
'reg_Phone': '123-342-1211',
'reg_Email': 'someemail@emai.com',
'reg_Company': 'None',
'reg_Address1': 'Some place Dr',
'reg_Address2': '',
'reg_City': 'Mars',
'reg_State': 'GA',
'reg_Zip': '12121',
'reg_Country': 'United States'
我将如何在 C# 中做同样的事情?谢谢!