Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用.NET以编程方式将信息发送到C#中的web服务?_C#_.net_Web Services_Webrequest - Fatal编程技术网

如何使用.NET以编程方式将信息发送到C#中的web服务?

如何使用.NET以编程方式将信息发送到C#中的web服务?,c#,.net,web-services,webrequest,C#,.net,Web Services,Webrequest,我知道这算是重新发明了轮子,但我需要知道如何通过http/soap/xml和web消息与web服务通信。原因是我需要与第三方web服务通信才能工作,但是WSDL或其他东西有问题,当使用.NET向导连接到它时,它不工作 那么,有人能给我一个过程/简单的例子/如何做到这一点,或者有人能给我一个链接到某个地方来解释它吗?我对网络请求和响应不是很在行 如何构造和发送请求?如何解析响应 下面是一个简单web服务的代码。假设.asmx的地址为“http://www.mwebb.com/TestSimpleS

我知道这算是重新发明了轮子,但我需要知道如何通过http/soap/xml和web消息与web服务通信。原因是我需要与第三方web服务通信才能工作,但是WSDL或其他东西有问题,当使用.NET向导连接到它时,它不工作

那么,有人能给我一个过程/简单的例子/如何做到这一点,或者有人能给我一个链接到某个地方来解释它吗?我对网络请求和响应不是很在行

如何构造和发送请求?如何解析响应

下面是一个简单web服务的代码。假设.asmx的地址为“http://www.mwebb.com/TestSimpleService.asmx“:

我怎么称呼这个方法

感谢您的帮助

编辑

我真的只是想知道如何将数据发送到web服务。我可以获得所有的方法/SOAP操作/URL数据,并且可以解析响应数据。我只是不知道该用什么东西或者怎么用

如果有人知道一些简单的.NET soap客户端,比如Python中的SUD,那也会有所帮助。

有一个允许动态创建绑定的工具

例如(他们网站上的一个例子):


如果您想直接通信,我会考虑使用HTTPWebRequest,因为webservice调用最终只是使用HTTP POST发送的XML

以下链接有一些示例:

作为一种在以编程方式与.net联系之前测试外部Web服务的方法,一种方法是使用测试工具,如生成您认为需要发布到Web服务的确切XML,并使用该工具手动发送它

然后您可以开发与.net等效的

编辑-下面是一个快速示例,我根据上面的链接调用了您的示例服务(使用SOAP1.2):

        {
            string soap = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
   xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
   xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
  <soap:Body>
    <SayHello xmlns=""http://tempuri.org/"">
      <name>My Name Here</name>
    </SayHello>
  </soap:Body>
</soap:Envelope>";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:2439/Soap.asmx");
            req.ContentType = "application/soap+xml;";
            req.Method = "POST";

            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soap);
                }
            }

            WebResponse response = req.GetResponse(); 
            Stream responseStream = response.GetResponseStream();

            // Do whatever you need with the response
            Byte[] myData = ReadFully(responseStream);
            string s = System.Text.ASCIIEncoding.ASCII.GetString(myData);
        }
{
字符串soap=@”
我的名字在这里
";
HttpWebRequest req=(HttpWebRequest)WebRequest.Create(“http://localhost:2439/Soap.asmx");
req.ContentType=“应用程序/soap+xml;”;
请求方法=“POST”;
使用(Stream stm=req.GetRequestStream())
{
使用(StreamWriter stmw=新StreamWriter(stm))
{
stmw.Write(soap);
}
}
WebResponse=req.GetResponse();
Stream responseStream=response.GetResponseStream();
//对响应做任何你需要的事情
字节[]myData=readfull(responseStream);
字符串s=System.Text.ascienceoding.ASCII.GetString(myData);
}

ReadFully方法来自Jon Skeet,看起来像是源于Jon Skeet。

如果您的服务真的像您的示例一样简单,那么只需使用“添加服务引用”并使用代理即可

如果不起作用,则使用命令行svcutil.exe程序并发布它打印的错误消息


除非别无选择,否则不要使用WSDL.EXE。

所选答案的代码对我不起作用。我不得不在标题中添加
SOAPAction
,同时更改
ContentType

以下是完整的代码:

var strRequest = @"<soap12:Envelope> 
                    ... 
                    </soap12:Envelope>";

string webServiceUrl = "http://localhost:8080/AccontService.svc";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webServiceUrl);

request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";         
request.Accept = "text/xml";
request.Headers.Add("SOAPAction", "http://tempuri.org/IAccountService/UpdateAccount");

byte[] data = Encoding.UTF8.GetBytes(strRequest);

request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseXmlString = reader.ReadToEnd();

return new HttpResponseMessage()
{
    Content = new StringContent(responseXmlString, Encoding.UTF8, "application/xml")
};
var strequest=@”
... 
";
字符串webServiceUrl=”http://localhost:8080/AccontService.svc";
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(webServiceUrl);
request.Method=“POST”;
request.ContentType=“text/xml;charset=UTF-8”;
request.Accept=“text/xml”;
添加(“SOAPAction”http://tempuri.org/IAccountService/UpdateAccount");
byte[]data=Encoding.UTF8.GetBytes(strRequest);
request.ContentLength=data.Length;
Stream requestStream=request.GetRequestStream();
requestStream.Write(数据,0,数据长度);
requestStream.Close();
HttpWebResponse=(HttpWebResponse)request.GetResponse();
StreamReader=新的StreamReader(response.GetResponseStream(),Encoding.UTF8);
string responseXmlString=reader.ReadToEnd();
返回新的HttpResponseMessage()
{
Content=newstringcontent(responseXmlString,Encoding.UTF8,“应用程序/xml”)
};

@Mike您不需要在VS中使用WSDL;如果您至少可以通过浏览器访问wsdl,然后将wsdl下载到本地磁盘,那么您可以使用wsdl.exe。如果您需要一种使用Web服务的VS less方法,那么您可以使用wsdl.exe:但这仍然使用wsdl。wsdl有一些问题,所以我真的无法使用它。在.NET中必须有一种使用http/soap请求/响应对象发送/接收数据的方法。或者必须有一个简单的库来实现这一点,比如Python中的SUD。VS是否告诉您WSDL有什么问题?Web服务是否以某种形式进行了保护?例如,如果它是一个使用WSE 2.0的旧.net 1.1 Web服务,则.net的更高版本在使用WCFOh进行互操作时会遇到问题,但没有意识到这有区别。显示了我在这个问题上的学识:)是的,我试过了,但没有成功。我猜这就是为什么。所以不可能通过这个XMLRPC库使用SOAP<代码>在响应周围使用语句?
        {
            string soap = @"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" 
   xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" 
   xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
  <soap:Body>
    <SayHello xmlns=""http://tempuri.org/"">
      <name>My Name Here</name>
    </SayHello>
  </soap:Body>
</soap:Envelope>";

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:2439/Soap.asmx");
            req.ContentType = "application/soap+xml;";
            req.Method = "POST";

            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(soap);
                }
            }

            WebResponse response = req.GetResponse(); 
            Stream responseStream = response.GetResponseStream();

            // Do whatever you need with the response
            Byte[] myData = ReadFully(responseStream);
            string s = System.Text.ASCIIEncoding.ASCII.GetString(myData);
        }
var strRequest = @"<soap12:Envelope> 
                    ... 
                    </soap12:Envelope>";

string webServiceUrl = "http://localhost:8080/AccontService.svc";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(webServiceUrl);

request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";         
request.Accept = "text/xml";
request.Headers.Add("SOAPAction", "http://tempuri.org/IAccountService/UpdateAccount");

byte[] data = Encoding.UTF8.GetBytes(strRequest);

request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string responseXmlString = reader.ReadToEnd();

return new HttpResponseMessage()
{
    Content = new StringContent(responseXmlString, Encoding.UTF8, "application/xml")
};