0

I need imitate AJAX call to web service in my console application. Is there any way to do this with HttpWebRequest?

Source request:

var webRequest = Sys.Net.WebServiceProxy.invoke('http://webserver.com/WS_Message.asmx', 'MyMethod', false, {p1:aa,p2:bb,p3:123}, onSuccess, onFailure, userContext, timeout, enableJsonp, jsonpCallbackParameter);

Sample that doesn't work:

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());
}
2
  • 1
    Could you post some more information about what you expecting and what you are currently getting back? Commented Dec 9, 2013 at 9:53
  • Already solved my issue. Was related to incorrect headers. Commented Dec 9, 2013 at 10:16

2 Answers 2

1

It looks like you were calling into a (.NET based) web service in javascript. Why not simply add a web reference to your console app, and call it that way ?

It will be much less work than trying to replicate the web service call manually through an HttpWebRequest.

Sign up to request clarification or add additional context in comments.

Comments

0

I had to use Chrome Developer Console to see correct http headers. My issue was related to incorrect JSON string format.

@"{""p1"": ""aa"", ""p2"": ""bb"", ""p3"": 123}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.