Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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...
C#
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...
C#
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);
    dataStream.Close();
}
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

1 solution

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.
 
Share this answer
 
Comments
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):
<system.net>
<defaultproxy enabled="true">
<proxy bypassonlocal="False" proxyaddress="http://127.0.0.1:8888"/>
</defaultproxy>
</system.net>
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.

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