我可以在WCF服务中使用wsHttpBinding来获取HTTP POST请求吗? [英] Can I use wsHttpBinding in WCF Service for HTTP POST requests?

查看:134
本文介绍了我可以在WCF服务中使用wsHttpBinding来获取HTTP POST请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的WCF服务,具有自定义用户名 - 密码身份验证。我有一个使用此服务的Windows窗体应用程序,它工作正常。我需要知道如何使用HTTP POST请求通过ASP.Net或具有相同wsHttpBinding的任何HTML页面调用此服务?我尝试了webHttpBinding(因为我认为它应该工作)但是身份验证不起作用,并且可以在没有用户凭据的情况下访问服务,这是不可接受的。在这种情况下,我无法获得wsHttpBinding工作。我可以这种方式使用这项服务......

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");



但是我必须通过HTTP POST请求以另一种方式调用它,为此目的,我需要这样的东西。 ..


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);
    dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();



我无法通过HttpWebRequest使用该服务,任何人都可以请帮帮我吗? (basicHttpBinding也因为缺乏安全性而无法接受)


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)

推荐答案

从技术上讲,您可以手动创建HTTP POST并与WCF服务进行通信,代理类就可以为您完成。

题是正确手动创建请求的内容,特别是与SOAP。



我也注意到,尝试特殊照顾派json数据,但是你将ContentType设置为application / soap + xml,那就不行了。



如果你真的需要这样做一方面,你可以试试这个:

。使用代理类创建一个客户端,并使用Fiddler捕获客户端和服务之间的通信

你马上就能看到到底如何。请求看起来像(标题和内容),然后使用HttpWebRequest实现它。
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.


这篇关于我可以在WCF服务中使用wsHttpBinding来获取HTTP POST请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆