There is a code:

var client = new RestClient("http://api2.online-convert.com/jobs"); var request = new RestRequest(Method.POST); request.AddHeader("cache-control", "no-cache"); request.AddHeader("x-oc-api-key", "<your API key here>"); request.AddParameter("application/json", "{\"input\":[{\"type\":\"remote\",\"source\":\"http://static.online-convert.com/example-file/raster%20image/jpg/example_small.jpg\"}],\"conversion\":[{\"category\":\"image\",\"target\":\"png\"}]}", ParameterType.RequestBody); IRestResponse response = client.Execute(request); 

But it is used on the old .Net version. The new class has no RestClient and RestRequest. I began to redo:

 HttpWebRequest request = null; request = (HttpWebRequest)WebRequest.Create("http://api2.online-convert.com/jobs"); request.Accept = @"application/json;text/xml"; request.Method = "POST"; Stream requestStream = request.GetRequestStreamAsync().Result; 

But I do not know how I use the parameters:

 request.AddParameter("application/json", "{\"input\":[{\"type\":\"remote\",\"source\":\"http://static.online-convert.com/example-file/raster%20image/jpg/example_small.jpg\"}],\"conversion\":[{\"category\":\"image\",\"target\":\"png\"}]}", ParameterType.RequestBody); 

    1 answer 1

    From here :

      // POST a JSON string void POST(string url, string jsonContent) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); Byte[] byteArray = encoding.GetBytes(jsonContent); request.ContentLength = byteArray.Length; request.ContentType = @"application/json"; using (Stream dataStream = request.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } long length = 0; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { length = response.ContentLength; } } catch (WebException ex) { // Log exception and throw as for GET example above } } 

    Call the function to send the request:

     var Parameters; // ... заполняем параметры динамически var serializer = new JavaScriptSerializer(); string RequestParameters = serializer.Serialize(Parameters); // Отправляем запрос string Response = this.Post("http://api2.online-convert.com/jobs", RequestParameters); MyResponse Response = serializer.Deserialize<MyResponse>(Response); 

    Alternatively you can use the RestSharp library.

    • HttpWebRequest no HttpWebRequest property - Viktor Bylbas
    • You did not say in which version of .NET you are working, the “old version” says little as a fact. - Daniel Protopopov
    • I am using 4.6.x - Viktor Bylbas
    • In this case, I ask here - msdn.microsoft.com/ru-ru/library/… - Daniel Protopopov