如何使用 webRequest Class 多个命令在 C# 中发送 cURL/API 请求
How to send cURL/API requests in C# with webRequest Class Multible Commands
我需要将此发送给服务器:
curl https://api.placetel.de/api/getRouting.xml \
-d 'api_key=xxxxxxxxxxxxxxxxxxx' \
-d 'number=068111111XXX'
如果我尝试:
WebRequest request = WebRequest.Create("https://api.placetel.de/api/getRouting.xml");
request.Method = "POST";
string postData = "api_key=XXXXXXXXXXXXXXX number=0685123XXXXXX";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
listBox1.Items.Add(responseFromServer);
我收到登录错误 - api 不正确,因为我的程序在 "one line"
中发送了 api + number=
问题是我如何像在两个命令中一样发送它?我尝试使用字符串 postData 和 postData1 aso - 这不起作用
赞:
string postData1 = "\nnumber=068567909000";
byte[] byteArray1 = Encoding.UTF8.GetBytes(postData1);
添加 ContentType
并用符号分隔每个参数:
request.ContentType = "application/x-www-form-urlencoded";
var postData = "api_key=XXXXXXXXXXXXXXX";
postData += "&number=0685123XXXXXX";
我需要将此发送给服务器:
curl https://api.placetel.de/api/getRouting.xml \
-d 'api_key=xxxxxxxxxxxxxxxxxxx' \
-d 'number=068111111XXX'
如果我尝试:
WebRequest request = WebRequest.Create("https://api.placetel.de/api/getRouting.xml");
request.Method = "POST";
string postData = "api_key=XXXXXXXXXXXXXXX number=0685123XXXXXX";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
listBox1.Items.Add(responseFromServer);
我收到登录错误 - api 不正确,因为我的程序在 "one line"
中发送了 api + number=问题是我如何像在两个命令中一样发送它?我尝试使用字符串 postData 和 postData1 aso - 这不起作用
赞:
string postData1 = "\nnumber=068567909000";
byte[] byteArray1 = Encoding.UTF8.GetBytes(postData1);
添加 ContentType
并用符号分隔每个参数:
request.ContentType = "application/x-www-form-urlencoded";
var postData = "api_key=XXXXXXXXXXXXXXX";
postData += "&number=0685123XXXXXX";