使用具有自定义身份验证的HttpWebRequest调用WCF服务 [英] Calling a WCF service with HttpWebRequest having custom authentication

查看:106
本文介绍了使用具有自定义身份验证的HttpWebRequest调用WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里但我的情况有点不同。



1)它不是REST服务

2)它有自定义用户名 - 密码验证



我需要的是使用带有JSON请求和响应格式的HttpWebRequestPOST方法来调用服务;这就是我到目前为止...



  public  < span class =code-keyword> class 服务:IService 
{
public string SayHello( string name)
{
return < span class =code-sdkkeyword> String .Format( Hello {0}!,name);
}
}





  public   interface  IService 
{
[OperationContract]
string SayHello( string name);
}



我需要的是这样的......

 HttpWebRequest请求= WebRequest.Create(  http:// localhost:56779 / BeSTService.svc / SayHello)< span class =code-keyword> as  HttpWebRequest; 
string parameters = {\ name\ :\ Umais\ }; // 或任何其他发送参数的方式
request.Method = POST;
request.ContentLength = 0 ;
request.ContentType = application / json;
if (!string.IsNullOrEmpty(parameters))
{
byte [] byteArray = Encoding.UTF8.GetBytes(参数);
request.ContentLength = byteArray.Length;
流dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0 ,byteArray.Length);
dataStream.Close();
}
HttpWebResponse response =(HttpWebResponse)request.GetResponse();



我需要使用wsHttpBinding,因为我无法实现webHttpBinding中的相同身份验证。 wsHttpBinding在Windows应用程序中正在运行(带有身份验证);但是,我需要将此服务称为HttpWebResponse,HttpWebRequest。请帮帮我吧!我收到错误请求错误。

解决方案

查看此 WCF REST - WsHttpbinding& WebHttpbinding [ ^ ]

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.

解决方案

View this WCF REST - WsHttpbinding & WebHttpbinding[^]


这篇关于使用具有自定义身份验证的HttpWebRequest调用WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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