通过SOAP调用.svc

时间:2018-03-29 04:23:14

标签: c# soap httpwebrequest soap-client

我想通过Soap为.svc编写测试。任何人都可以帮我创建HTTPWebRequest来调用.svc动作。

下面是我的示例代码

 var url = "http://localhost:4000/SI.WS.Core/SIF/CaseService.svc";
  var action = "http://localhost:4000/SI.WS.Core/SIF/CaseService.svc/GetCases";

 internal static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }

1 个答案:

答案 0 :(得分:0)

internal  HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            var username = "xxxxx";
            var password = "xxxxx";


            byte[] credentialBuffer = new UTF8Encoding().GetBytes(
                    username + ":" +
                    password);
            webRequest.Headers["Authorization"] =
                    "Basic " + Convert.ToBase64String(credentialBuffer);

            webRequest.PreAuthenticate = true;
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";

            webRequest.Method = "POST";
            return webRequest;
        }
相关问题