0

I am trying to invoke(HTTP POST request) a REST web service from C# code passing json input. Below is the code i am using.

            MyModel myModelObj = new MyModel();
            myModelObj.code = "0003";
            myModelObj.qty = "3";
            var serializer = new JavaScriptSerializer();
            var jsonInput = serializer.Serialize(myModelObj);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(jsonInput);
            request.Method = "POST";
            request.ContentLength = byteArray.Length;
            request.ContentType = @"application/json"; 
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
            try
            {
                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseValue = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                // Log exception and throw as for GET example above
            }

When I run this code am getting '(400) bad request' error, but when i try to invoke the same service using a jquery ajax call it worked. below is the jquery code used for invoking.

        $.ajax({
            type: "POST",
            url:"XXXXXXXXXXXXXXXXXXXXXXX",
            dataType : "json",
            data: {code:"0003", qty: "3"}
        }).done(function(response, status, request) {
            console.log("add cart :: ");
            console.log(response);
            console.log(request);
            updateCartCount();
        }).fail(function(error) {
            console.log(error);
        });

The service used in both the cases is same and the json input paramaters are also same, but I am able to get response using jquery but not in c#. For some reasons I had to implement this in C# only. Is there anything I need to change in the C# code and first of all whether my approach is correct? Can someone please help me on this? Thanks in advance! I am using .NET framework 4.5 with MVC 5. The line at which the exception raised is "using (var response = (HttpWebResponse)request.GetResponse())"

Pranay
  • 442
  • 6
  • 14
  • Is both these applications running on the same domain? Cross domain restriction may be one thing to look at! – Darshana Nov 17 '14 at 04:40
  • Both the client application and the REST service are running on two different machines but with in the same corporate network. If this is a cross domain restriction, will it work through javascript? becoz am able to get response through an ajax call. Is there any way to check if it is restricted? – Pranay Nov 17 '14 at 05:34
  • 1
    Install Fiddler and use that to compare the two requests. That should enable you to find the difference. – Jørn Wildt Nov 17 '14 at 12:40
  • Like Jorn mentioned Fiddler is your friend. I would also use Microsoft Network Monitor to see if the packets hitting the machine at all where the service was installed. Check the grants for the ports and accessibility of the machine too in the router configuration. But the request is generated within the network these are irrelevant. – Darshana Nov 17 '14 at 20:48

0 Answers0