I have written a simple web service in Visual Studio 2012.
When I test the web service in internet explorer I get the following result.
<?xml version="1.0" encoding="UTF-8"?>
<string xmlns="http://tempuri.org/">[{"ID":1,"ConsultantName":"Jim",
"ConsultantEmailAddress":"Jim@xyz.com","ConsultantCellNumber":null,
"ConsultantPhoneNumber":null},{"ID":2,"ConsultantName":"Steve","ConsultantEmailAddress":"Steve@xyz.com",
"ConsultantCellNumber":null,"ConsultantPhoneNumber":null}]</string>
two records returned in JSON format
all looks fine
So now to call the web service in XAMARIN:
- 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.
- I looked at all the examples I could find on consuming web services and eventually settled on the modified code below:
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.
This is the line where the error occurs:
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
(This line is right at the bottom of the code included)
error - // Remote server returned an error: (400) Bad Request
The code is below: I would appreciate any comments or guidance.
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.
Thank you in anticipation.
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();
}