none
Updating from 3.5 to 4.5 HTTPClient RRS feed

  • Question

  • I'm trying to update some old code from 3.5 to 4.5. Not sure how to update this code. I'm a little newb to http client:

    try
            {
                string json = JsonConvert.SerializeObject(obj);
                var client = new HttpClient();
                var webRequest = WebRequest.Create(uri);
    
                webRequest.Method = "POST";
                webRequest.ContentType = "application/json";
    
                using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    streamWriter.Write(json);
                }
    
                var httpResponse = (HttpWebResponse)webRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    _dt = JsonConvert.DeserializeObject<DataTable>(result);
                }
            }
            catch (Exception ex)
            {
                Debug.Log("Failed To Load Tile Data");
            }

    Wednesday, May 15, 2019 12:27 AM

All replies

  • Hi     Joined Nov 2011,

    Thank you for posting here.

    When you update from 3.5 to 4.5 ,I suggest you can try to use the System.Net.Http.HttpClient class.

    From your description ,I have made a sample on my side, and I use the method SendJsonPost() to  send post message.

    you can refer  it and modify it.

    Here’s the code:

      public static async Task<string> SendSJsonPost(Object Jsondata, string uri)  // JsonData is a class which used to generate Json data
            {
                string json = JsonConvert.SerializeObject(obj);
                var client = new HttpClient();
    
                HttpContent httpContent = new StringContent(json, Encoding.UTF8, "application/json"); // Encoding.ASCII and so on, depending on your need.
                var reponse = await client.PostAsync(uri, httpContent);
    
                var result = await reponse.Content.ReadAsStringAsync();
                return result;
            }
                    
    Hope it can help you.
                    

    Best Regards

    Yong Lu

              

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by old_School Tuesday, May 21, 2019 3:25 PM
    • Unmarked as answer by old_School Tuesday, May 21, 2019 3:25 PM
    Wednesday, May 15, 2019 7:56 AM
    Moderator
  • That makes sense but I need to convert the response into a data table. In your sample its reading it as a string. Would it be like this for my code snippet?

    var reponse = await client.PostAsync(uri, httpContent);
    var result = await JsonConvert.DeserializeObject<DataTable>(reponse.Content());
                return result;

    Not sure how you would convert the response to back to an object and return it 

    Tuesday, May 21, 2019 3:32 PM
  • Hi  old_School,  

    According to your question, you can refer the following code:

    string result = await reponse.Content.ReadAsStringAsync(); 
    
    _dt = JsonConvert.DeserializeObject<DataTable>(result); // deserialize the ‘result’ to DataTale 

    Besides, not all data can use ‘JsonConvert.DeserializeObject<T>(string value)’ method to convert to DataTable, 
    In order to convert the response into a data table,  ‘result’ in the code should be type of string and represent The JSON to deserialize.

    For example, here’s the code for the ‘JsonConvert.DeserializeObject<T>(string value)’ test:

    private void button1_Click(object sender, EventArgs e)
            { 
                string jsonDt = JsonConvert.SerializeObject(dt);  // dt is a DataTable which have some data.
                MessageBox.Show(jsonDt);
                DataTable _dt = JsonConvert.DeserializeObject<DataTable>(jsonDt);
                dataGridView1.DataSource = _dt;   // use DataGridView to show the result. 
            }
                

    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 23, 2019 8:45 AM
    Moderator