question

Joe-0222 avatar image
0 Votes"
Joe-0222 asked JessieZhang-2116 answered

Alternative to Javascript's XMLHttpRequest in C# with Xamarin

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.

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JessieZhang-2116 avatar image
1 Vote"
JessieZhang-2116 answered

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://docs.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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SimpleSamples avatar image
1 Vote"
SimpleSamples answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.