Is it possible to mimic an AJAX call to a web service within a console application using HttpWebRequest?
Here is the source request:
var webRequest = Sys.Net.WebServiceProxy.invoke('', 'MyMethod', false, {p1:aa,p2:bb,p3:123}, onSuccess, onFailure, userContext, timeout, enableJsonp, jsonpCallbackParameter);
Below is a sample code snippet that attempts to do this but is not functional:
HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://webserver.com/WS_Message.asmx/MyMethod");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
byte[] _bytes= Encoding.UTF8.GetBytes("{p1:aa,p2:bb,p3:123}");
request.ContentLength = _bytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(_bytes, 0, _bytes.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
Console.WriteLine(reader.ReadToEnd());
}