Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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...

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

and
C#
public interface IService
{
    [OperationContract]
    string SayHello(string name);
}

What I need to have is something like this...
C#
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);
                dataStream.Close();
            }
            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
Updated 15-Jan-13 0:54am
v2
Comments
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

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900