简体   繁体   English

通过httpWebRequest调用WCF服务时,URL问题

[英]problem with url when calling wcf service by httpWebRequest

When I call manually wcf service what should I type in url place : 当我手动调用wcf服务时,应在url位置键入什么:

HttpWebRequest httpWebRequest = WebRequest.Create(url)as HttpWebRequest;

should be there url to my svc file 应该在那里我的svc文件的URL

http://localhost/service/LMTService.svc

or wsdl 或wsdl

http://localhost/service/LMTService.svc?wsdl

or url to service action ? 或服务行动网址?

http://localhost/service/LMTService.svc/soap/GetSerializedSoapData

It depends on the binding of the endpoint. 这取决于端点的绑定。 If using a SOAP binding (ie, basicHttpBinding , wsHttpBinding , etc), the request URI should be the endpoint address (not the service address). 如果使用SOAP绑定(即basicHttpBindingwsHttpBinding等),则请求URI应该是端点地址(而不是服务地址)。 Also, in some SOAP versions (such as SOAP11, used in basicHttpBinding ), you need to specify the action as a HTTP header. 此外,在一些SOAP版本(如SOAP11,在使用basicHttpBinding ),你需要指定动作的HTTP标头。 If you're using webHttpBinding (with webHttp behavior) the address is the address of the endpoint, plus the UriTemplate (which by default is just the method name) of the operation you want to call. 如果您使用的是webHttpBinding (具有webHttp行为),则该地址是端点的地址,再加上要调用的操作的UriTemplate(默认情况下只是方法名称)。

The code below shows a HttpWebRequest -based request being sent to two endpoints, a one using BasicHttpBinding , one using WebHttpBinding . 下面的代码显示了一个基于HttpWebRequest的请求被发送到两个端点,一个使用BasicHttpBinding ,一个使用WebHttpBinding

public class StackOverflow_7525850
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        int Add(int x, int y);
    }
    public class Service : ITest
    {
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    public static string SendRequest(string uri, string method, string contentType, string body, Dictionary<string, string> headers)
    {
        string responseBody = null;

        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.Method = method;
        if (headers != null)
        {
            foreach (string headerName in headers.Keys)
            {
                req.Headers[headerName] = headers[headerName];
            }
        }
        if (!String.IsNullOrEmpty(contentType))
        {
            req.ContentType = contentType;
        }

        if (body != null)
        {
            byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
            req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
            req.GetRequestStream().Close();
        }

        HttpWebResponse resp;
        try
        {
            resp = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException e)
        {
            resp = (HttpWebResponse)e.Response;
        }

        if (resp == null)
        {
            responseBody = null;
            Console.WriteLine("Response is null");
        }
        else
        {
            Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
            foreach (string headerName in resp.Headers.AllKeys)
            {
                Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
            }
            Console.WriteLine();
            Stream respStream = resp.GetResponseStream();
            if (respStream != null)
            {
                responseBody = new StreamReader(respStream).ReadToEnd();
                Console.WriteLine(responseBody);
            }
            else
            {
                Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
            }
        }

        Console.WriteLine();
        Console.WriteLine("  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  ");
        Console.WriteLine();

        return responseBody;
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "basic");
        host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "web").Behaviors.Add(new WebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        string soapBody = @"<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
  <s:Body>
    <Add xmlns=""http://tempuri.org/"">
      <x>44</x>
      <y>55</y>
    </Add>
  </s:Body>
</s:Envelope>";
        SendRequest(baseAddress + "/basic", "POST", "text/xml", soapBody, new Dictionary<string, string> { { "SOAPAction", "http://tempuri.org/ITest/Add" } });

        SendRequest(baseAddress + "/web/Add", "POST", "text/xml", "<Add xmlns=\"http://tempuri.org/\"><x>55</x><y>66</y></Add>", null);

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM