0

I'm going to test an sms service like below, but I want it to run async. I prepared a code like below, but how can I make it work as async-await?

private async void Form1_Load(object sender, EventArgs e)
{
    var url = "http://panel.com/smsgonder1Npost.php";

    //string tur = "Normal";
    //if (turkce.Checked == true) tur = "Turkish";
    string sms1N = "data=<sms><kno>" + kno.Text + "</kno>" +
        "<kulad>" + kad.Text + "</kulad>" +
        "<sifre>" + ksifre.Text + "</sifre>" +
        "<gonderen>" + "SMS TEST" + "</gonderen>" +
        "<mesaj>" + "test message" + "</mesaj>" +
        "<numaralar>" + "number" + "</numaralar>" +
        "<tur>" + "Normal" + "</tur></sms>";

    await XmlPost(PostAddress: url, xmlData: sms1N);
}

private async Task<string> XmlPost(string PostAddress, string xmlData)
{
    using (WebClient wUpload =  new WebClient())
    {
        wUpload.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        textBox1.AppendText(xmlData);

        Byte[] bPostArray = Encoding.UTF8.GetBytes(xmlData);
        Byte[] bResponse = wUpload.UploadData(PostAddress, "POST", bPostArray);
        Char[] sReturnChars = Encoding.UTF8.GetChars(bResponse);

        string sWebPage = new string(sReturnChars);
        textBox1.AppendText(sWebPage);

        return  sWebPage;
    }
}

Added new hi, i added new code below. Does it work like httpClient? Or can you give an example of httpClient?

  public async string postXMLData(string destinationUrl, string requestXml)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);
        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = bytes.Length;
        request.Method = "POST";
        Stream requestStream =  request.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
        requestStream.Close();
        HttpWebResponse response;
        response = (HttpWebResponse)request.GetResponse();
        if (response.StatusCode == HttpStatusCode.OK)
        {
            Stream responseStream = response.GetResponseStream();
            string responseStr = new StreamReader(responseStream).ReadToEnd();
            textBox1.AppendText(responseStr);

            return responseStr;
        }
        return null;
    }
johnvayne
  • 79
  • 3
  • 8

2 Answers2

3

how can I make it work as async-await

By replacing the WebClient with HttpClient.

HttpClient has (only) async methods. WebClient is synchronous.

And a side note, don't use using with HttpClient.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    it's also noteworthy that `WebClient` is obsolete for quite some time now. – Franz Gleichmann Jul 19 '21 at 17:01
  • 1
    Well, it's not [officially](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-5.0) obsolete, just not recomended [for new software]. But when you want sync I/O or FTP it might still be an option. It was ported to Core (.net 5). – H H Jul 19 '21 at 17:11
  • *WebClient is synchronous*: this class provides awaitable asynchronous methods, e.g., [UploadDataTaskAsync()](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploaddatataskasync), besides the event-driven methods (as [UploadDataAsync()](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploaddataasync)). – Jimi Jul 19 '21 at 17:29
  • I added a new code block above. but I couldn't run it as async. can you help me ? – johnvayne Jul 19 '21 at 17:44
  • That has been answered already: https://stackoverflow.com/questions/25352462/how-to-send-xml-content-with-httpclient-postasync and many more. You will need much less code. – H H Jul 19 '21 at 18:28
  • I'm a bit unsure about your content requestXml and x-www-form-urlencoded? But whatever works for you. – H H Jul 19 '21 at 18:29
  • `WebClient` is asynchronous. It has always been with EAP and now with TAP. – Paulo Morgado Jul 20 '21 at 08:02
2

A simple change to your code would be to use the WebClient.UploadDataTaskAsync Method.

WebClient, introduced in .NET Framework 1.1, has been asynchronous since .NET Framework 2.0.

It was asynchronous using Event-based Asynchronous Pattern (EAP) (the Async suffixed methods).

Since .NET Framework 4.5, it has been augmented with Task-based asynchronous pattern (TAP) (the TaskAsync suffixed methods).

You can see here how WebClient.UploadDataTaskAsync has been implemented using WebClient.UploadDataAsync.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59