简体   繁体   中英

Error in using WebClient object REST API call using C#

I am trying to POST an order to REST API using the below code:

 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;
            }

I am getting the following exception:

Input String was not in a correct format.

Any help is greatly appreciated.

There should be a

?

after the orderId before the parameters' string. Also try URL Encoding

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

and

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM