Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WCF
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 23:28pm
Edited 15-Jan-13 1:54am
v2
Comments
Umais ASGHAR at 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
0 OriginalGriff 8,284
1 Sergey Alexandrovich Kryukov 7,327
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,920


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
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