如何使用webRequest类Multible命令在{C#中发送cURL / API请求

时间:2015-07-22 14:12:10

标签: c# curl webrequest

我需要发送一个这样的服务器:

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不对,因为我的程序在“一行”中发送api + number =

问题是如何像两个命令一样发送它?我尝试使用字符串postData& postData1 aso - 不起作用

喜欢:

string postData1 = "\nnumber=068567909000";
  byte[] byteArray1 = Encoding.UTF8.GetBytes(postData1);

1 个答案:

答案 0 :(得分:0)

添加ContentType并使用&符号分隔每个参数:

request.ContentType = "application/x-www-form-urlencoded";

var postData = "api_key=XXXXXXXXXXXXXXX";
postData += "&number=0685123XXXXXX";