Alternative to Javascript's XMLHttpRequest in C# with Xamarin

Joe 41 Reputation points
2021-04-13T22:15:01.503+00:00

I have a webpage that uses XMLHttpRequest to do things like upload files, update the page, communication. I am trying to make an app (Android and iOS) with Xamarin that could be used instead of this website however I am having a hard time figuring out how to do the things I did with XMLHttpRequest in C#. Here is an example of something in Javascript that I would need to do in C# in Xamarin.

    {
        link = new ExEmElHttpRequest();     
        x = Date.now();
        link.ooooooopppppppen('GET', 'ajax_FMU0104;' + x, true);

        link.oooooonnnnnnreadystatechange = function() {
            if (link.rrrrrrreeeeeadyState == 4 && link.ssssssttttttatus == 200) {
            flag = 1;
            }
        };
        link.ssssseeeeend(null);
    }

Is there a way to port this function to C# or any tools in C# that could accomplish the same thing? I had to change some of the code and function names in order to post the question without getting this weird access denied error. I'm guessing the site was thinking I was trying to inject code.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} votes

Accepted answer
  1. JessieZhang-MSFT 7,711 Reputation points Microsoft Vendor
    2021-04-16T06:01:00.51+00:00

    Hello,

    Welcome to Microsoft Q&A!

    A similar class in c# is HttpClient Class, which provides a class for sending HTTP requests and receiving HTTP responses from a resource identified by a URI.

    An examples:

    // HttpClient is intended to be instantiated once per application, rather than per-use. See Remarks.  
     static readonly HttpClient client = new HttpClient();  
    
    static async Task Main()  
    {  
      // Call asynchronous network methods in a try/catch block to handle exceptions.  
      try	  
      {  
         HttpResponseMessage response = await client.GetAsync("http://www.contoso.com/");  
         response.EnsureSuccessStatusCode();  
         string responseBody = await response.Content.ReadAsStringAsync();  
         // Above three lines can be replaced with new helper method below  
         // string responseBody = await client.GetStringAsync(uri);  
      
         Console.WriteLine(responseBody);  
      }  
      catch(HttpRequestException e)  
      {  
         Console.WriteLine("\nException Caught!");	  
         Console.WriteLine("Message :{0} ",e.Message);  
      }  
    }  
    

    You can also check document Consume a RESTful web service, which demonstrates how to consume a RESTful web service from a Xamarin.Forms application.
    Web service APIs that adhere to REST are called RESTful APIs, and are defined using:

    • A base URI.
    • HTTP methods, such as GET, POST, PUT, PATCH, or DELETE.
    • A media type for the data, such as JavaScript Object Notation (JSON).

    And there is also a sample about this, you can check it here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest .

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Sam of Simple Samples 5,546 Reputation points
    2021-04-13T22:30:11.003+00:00

    Perhaps you are looking for HttpClient; samples in HttpClient sample.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.