Click here to Skip to main content
13,458,661 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a simple WCF service with custom username-password authentication. I have a Windows Forms application that uses this service and it works fine. I need to know how can I call this service through an ASP.Net or any HTML page with same wsHttpBinding using HTTP POST request? I tried webHttpBinding (as I thought it should work) but the authentication does not work and service can be accessed without user credentials, which is unacceptable. I can't get wsHttpBinding work in this case. I can use this service this way...
ServiceClient proxy = new ServiceClient();
proxy.ClientCredentials.UserName.UserName = "umais";
proxy.ClientCredentials.UserName.Password = "asghar";
proxy.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None;
string reply = proxy.Hello("Umais");

but I have to call it in another way, through HTTP POST request, for that purpose, I need to have something like this...
string url = "http://localhost:56779/Service.svc/Hello";
string parameters = "{'Name': 'Umais'}";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = 0;
req.ContentType = "application/soap+xml; charset=utf-8";
if (!string.IsNullOrEmpty(parameters))
    byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
    req.ContentLength = byteArray.Length;
    Stream dataStream = req.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
HttpWebResponse response = (HttpWebResponse)req.GetResponse();

I'm unable to consume the service through HttpWebRequest, can anyone help me out here please? (basicHttpBinding is also unacceptable because of its lack of security)
Posted 16-Jan-13 22:49pm

1 solution

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

Solution 1

Technically you can create HTTP POST manually and communicate with WCF service, proxy class does exactly that for you.
Problem is to properly create content of the request manually, especially with SOAP.

I also noticed that you're trying to send json data, but you're setting ContentType to "application/soap+xml", that won't work.

If you really need to do this by hand, you can try this:
Create a client using proxy class and use Fiddler to capture communication between client and service.
You'll be able to see exactly how request looks like (both headers and content) and then implement it using HttpWebRequest.
Umais ASGHAR 17-Jan-13 7:18am
Yes, that code was just the sample, to show what kind of code I'm expecting to be used. I'll definitely use "application/json" instead of "application/soap+xml". I'll try your solution and see if I succeeed.
sjelen 17-Jan-13 7:42am
Also consider using basicHttpBinding, it supports transport level security (SSL) and in many cases that's enough. It's much easier to code manually.
Use wsHttpBinding only if you really need message level security or other WS extensions.
Umais ASGHAR 21-Jan-13 7:12am
can you guide me to use Fiddler? I don't see any traffic when I call this service through service client (proxy). How can I setup Fiddler to monitor my service calls?
Service url: Http://localhost:54253/Service.svc
Service name: string SayHello(string name)
I'm calling this service through a Windows Form Application, .Net 4.0
sjelen 21-Jan-13 12:26pm
Go to "Tools -> Fiddler Options -> Connections" and check "Act as system proxy..." and delete anything in "Bypass Fiddler for URLS ...."
Remember the listening port (8888 by default).
Add this to app.config of your client app (change the port if not default):
<defaultproxy enabled="true">
<proxy bypassonlocal="False" proxyaddress=""/>
Also you might need to add 'bypassProxyOnLocal="false"' attribute to your binding configuration.
Umais ASGHAR 22-Jan-13 3:54am
I tried all that, it didn't work so, I deployed my service on a web server and used Fiddler to monitor request/response and it works fine there. I read a statement somewhere on the internet that it does not work fine when using localhost. Anyway, it works now, thanks for the guidance.
sjelen 23-Jan-13 5:50am
You're welcome.

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 |
Web04-2016 | 2.8.180323.1 | Last Updated 17 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