使用C#使用WebClient对象REST API调用时出错

时间:2013-12-20 14:04:24

标签: c# api rest webclient uploadstring

我正在尝试使用以下代码将订单发布到REST API:

 string URI = "https://api.myTrade.com/s1/order/" + orderId;
            string myParameters = "symbol=AAPL&duration=day&side=buy&quantity=1&type=limit&price=1";
            Console.Write("Parameters : " + myParameters + "\n");
            using (WebClient wc = new WebClient())
            {
                wc.Headers[HttpRequestHeader.Authorization] = "Bearer " + token
                wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
                wc.Headers[HttpRequestHeader.Accept] = "application/json";
                string responsebody = wc.UploadString(URI, myParameters);
                //Console.Write("Output : " + responsebody + " ");
                dynamic dynObj = JsonConvert.DeserializeObject(responsebody);

                return responsebody;
            }

我收到以下异常:

Input String was not in a correct format.

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

应该有一个

  

在参数'字符串之前的orderId之后。还可以尝试URL编码

http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/zttxte6w(v=vs.110).aspx

希望它有所帮助。