title: Xamarin Forms - Consuming a Web Service written in Visual studio 2012
body: <p>I have written a simple web service in Visual Studio 2012.</p>
  
  <p>When I test the web service in internet explorer I get the following result.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;string xmlns="http://tempuri.org/"&gt;[{"ID":1,"ConsultantName":"Jim",
  "ConsultantEmailAddress":"Jim@xyz.com","ConsultantCellNumber":null,
  "ConsultantPhoneNumber":null},{"ID":2,"ConsultantName":"Steve","ConsultantEmailAddress":"Steve@xyz.com",
  "ConsultantCellNumber":null,"ConsultantPhoneNumber":null}]&lt;/string&gt;
  </code></pre>
  
  <p>two records returned in JSON format</p>
  
  <p>all looks fine</p>
  
  <p>So now to call the web service in XAMARIN:</p>
  
  <ol>
  <li>After a lot of research I found out that "LOCALHOST" means nothing in the ANDROID world - So I replace "LOCALHOST" with 10.0.2.2 and much more code got executed.</li>
  <li>I looked at all the examples I could find on consuming web services and eventually settled on the modified code below:</li>
  </ol>
  
  <p>In the code below it seems as if the request is successful when calling the web service however when the code tries to consume the result ( JSON string ) a  400 Bad Request  error occurs.</p>
  
  <p>This is the line where the error occurs:</p>
  
  <p>HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);</p>
  
  <p>(This line is right at the bottom of the code included)</p>
  
  <p>error - //  Remote server returned an error: (400) Bad Request</p>
  
  <p>The code is below:
  I would appreciate any comments or guidance.</p>
  
  <p>Further to this it would be great if XAMARIN could put out a real simple example of consuming a web service. Every example I have seen so far has complications where if your knowledge is minimal you have to do tons of research before you can even understand the code.
  What about a video with very simple code covering: Asynchronous Web consumption using XAMARIN Forms.
  The problem I face is that I have spend three days fiddling. If I had a simple working example I could slowly increase the complexity until I fully understand what is going on, but now I still do not have an idea where my problem lies.</p>
  
  <p>Thank you in anticipation.</p>
  
  <pre><code>    private async Task ExecuteLoadConsultants ()
      {
  
          Initialized = true;
          IsBusy = true;
  
          ConsultantInfo x = new ConsultantInfo{ ConsultantName = "WEwewewewe" };
              Consultants.Add (x);
  
          // call to webservice 
  
              HttpWebRequest webRequest = 
                         (HttpWebRequest)WebRequest.Create ("http://10.0.2.2:53498/TennantMobileWS1.asmx?op=GetConsultantAll");
          webRequest.UseDefaultCredentials = true;
          webRequest.Method = "POST";
          webRequest.Accept = "application/json";
          webRequest.ContentType = "application/json";
          webRequest.UseDefaultCredentials = true;
  
          webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
  
          IsBusy = false;
      }
  
  
  
      private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
      {
  
          HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
          // End the stream request operation
  
          Stream postStream = request.EndGetRequestStream(asynchronousResult);
  
          //Start the web request
          request.BeginGetResponse(new AsyncCallback(GetResponceStreamCallback), request);
  
      }
  
      void GetResponceStreamCallback(IAsyncResult callbackResult)
      {
          HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
  
  
          //  Remote server returned an error: (400) Bad Request on line below
  
  
          HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
          Stream streamResponse = response.GetResponseStream();
          StreamReader streamRead = new StreamReader(streamResponse);
          string responseString = streamRead.ReadToEnd();
  
      }
  </code></pre>