Click here to Skip to main content
13,551,695 members
Rate this:
Please Sign up or sign in to vote.
See more:
There is an answered question here but my case is a little different.

1) it's not a REST service
2) it has custom username-password validation

what I need is to call the service using HttpWebRequest "POST" method with JSON request and response format; this is what I have so far...

public class Service : IService
    public string SayHello(string name)
        return String.Format("Hello {0}!", name);

public interface IService
    string SayHello(string name);

What I need to have is something like this...
HttpWebRequest request = WebRequest.Create("http://localhost:56779/BeSTService.svc/SayHello") as HttpWebRequest;
            string parameters = "{\"name\":\"Umais\"}"; // or any other way to send parameters
            request.Method = "POST";
            request.ContentLength = 0;
            request.ContentType = "application/json";
            if (!string.IsNullOrEmpty(parameters))
                byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
                request.ContentLength = byteArray.Length;
                Stream dataStream = request.GetRequestStream();
                dataStream.Write(byteArray, 0, byteArray.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

I need to use wsHttpBinding as I'm unable to implement the same authentication in webHttpBinding. The wsHttpBinding is working (with authentication) in windows application; however, I need to call this service as HttpWebResponse,HttpWebRequest as well. Please help me out here! I get "Bad Request" error.
Posted 14-Jan-13 22:28pm
Updated 15-Jan-13 0:54am
Umais ASGHAR 15-Jan-13 4:47am
the exception occurs at the moment is "The underlying connection was closed: An unexpected error occurred on a receive." when I GetResponse() is called.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100