Click here to Skip to main content
12,949,858 members (93,839 online)
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);
    }
}

and
public interface IService
{
    [OperationContract]
    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);
                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 14-Jan-13 22:28pm
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

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

Solution 1

  Permalink  

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
OriginalGriff 5,489
CHill60 3,380
Maciej Los 2,913
Jochen Arndt 1,935
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web01 | 2.8.170524.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2017
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