httpWebRequest调用wcf服务时url出现问题

httpWebRequest调用wcf服务时url出现问题,wcf,httpwebrequest,Wcf,Httpwebrequest,当我手动调用wcf服务时,我应该在url位置键入什么: HttpWebRequest httpWebRequest = WebRequest.Create(url)as HttpWebRequest; 应该有我的svc文件的url http://localhost/service/LMTService.svc 或wsdl http://localhost/service/LMTService.svc?wsdl 或服务操作的url http://localhost/service/LMTSe

当我手动调用wcf服务时,我应该在url位置键入什么:

HttpWebRequest httpWebRequest = WebRequest.Create(url)as HttpWebRequest;
应该有我的svc文件的url

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

http://localhost/service/LMTService.svc?wsdl
或服务操作的url

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

它取决于端点的绑定。如果使用SOAP绑定(即
basicHttpBinding
wsHttpBinding
等),请求URI应该是端点地址(而不是服务地址)。此外,在某些SOAP版本中(如SOAP11,在
basicHttpBinding
中使用),您需要将操作指定为HTTP头。如果使用的是
webHttpBinding
(带有
webHttp
行为),则地址是端点的地址,加上要调用的操作的UriTemplate(默认情况下,它只是方法名)

下面的代码显示了一个基于
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();
    }
}
公共类堆栈溢出\u 7525850
{
[服务合同]
公共接口测试
{
[经营合同]
[WebInvoke(BodyStyle=WebMessageBodyStyle.WrappedRequest)]
整数加(整数x,整数y);
}
公共类服务:ITest
{
公共整数相加(整数x,整数y)
{
返回x+y;
}
}
公共静态字符串SendRequest(字符串uri、字符串方法、字符串contentType、字符串正文、字典头)
{
字符串responseBody=null;
HttpWebRequest req=(HttpWebRequest)HttpWebRequest.Create(uri);
要求方法=方法;
如果(标题!=null)
{
foreach(headers.Keys中的字符串headerName)
{
请求标头[标头名称]=标头[标头名称];
}
}
如果(!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;
尝试
{
resp=(HttpWebResponse)req.GetResponse();
}
捕获(WebE例外)
{
resp=(HttpWebResponse)e.Response;
}
如果(resp==null)
{
responseBody=null;
Console.WriteLine(“响应为空”);
}
其他的
{
WriteLine(“HTTP/{0}{1}{2}”,resp.ProtocolVersion,(int)resp.StatusCode,resp.StatusDescription);
foreach(分别在header.allkey中的字符串headerName)
{
WriteLine(“{0}:{1}”,headerName,resp.header[headerName]);
}
Console.WriteLine();
Stream respStream=resp.GetResponseStream();
if(respStream!=null)
{
responseBody=新的StreamReader(respStream).ReadToEnd();
控制台写入线(应答器体);
}
其他的
{
WriteLine(“HttpWebResponse.GetResponseStream返回null”);
}
}
Console.WriteLine();
Console.WriteLine(“*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*”;
Console.WriteLine();
返回响应体;
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
AddServiceEndpoint(typeof(ITest),new BasicHttpBinding(),“basic”);
host.AddServiceEndpoint(typeof(ITest),new-WebHttpBinding(),“web”).Behaviors.Add(new-WebHttpBehavior());
host.Open();
Console.WriteLine(“主机已打开”);
字符串soapBody=@”
44
55
";
SendRequest(baseAddress+“/basic”、“POST”、“text/xml”、soapBody、新字典{{“SOAPAction”,”http://tempuri.org/ITest/Add" } });
SendRequest(baseAddress+“/web/Add”,“POST”,“text/xml”,“5566”,null);
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}

感谢您的精彩解释;)我总是好奇人们是怎么知道这些的;)书籍、经验等。了解请求应该是什么的最简单方法是使用相同的绑定和服务器上的相同行为创建WCF客户端(使用
ChannelFactory
);然后使用该客户端发送请求,然后使用诸如Fiddler之类的工具查看客户端做了什么。