简体   繁体   English

是否可以使用没有 HttpWebRequest 的定义方法使用 C# 将数据发送到需要登录的特定 web 服务?

[英]Is it possible to send data to a specific login-required web service with C# using its defined method without HttpWebRequest?

I have access to WSDL file of a specific web service that contains a SendData method - basically I specify the TimeStamp and Value to be send.我可以访问包含 SendData 方法的特定 web 服务的 WSDL 文件 - 基本上我指定了要发送的时间戳和值。 I uploaded the WSDL file to my project in Visual Studio 2019 as a connected service (Add->Connected Service->Microsoft WCF Web Service Reference Provider->Browse->I added location of the WSDL file and specified the service that included SendData method). I uploaded the WSDL file to my project in Visual Studio 2019 as a connected service (Add->Connected Service->Microsoft WCF Web Service Reference Provider->Browse->I added location of the WSDL file and specified the service that included SendData method )。 After that I created a new client:之后我创建了一个新客户端:

ServiceSoapClient client = new ServiceSoapClient(ServiceSoapClient.EndpointConfiguration.ServiceSoap);

added credentials (the web service requires login):添加凭据(web 服务需要登录):

client.ClientCredentials.UserName.UserName = "test"; client.ClientCredentials.UserName.Password = "test";

created a new variable Data that contains TimeStamp and Value (in a specific format required by the method).创建了一个包含 TimeStamp 和 Value 的新变量 Data(以方法所需的特定格式)。

After that I invoked the method:之后我调用了该方法:

client.SendData(Data);

but it doesn't work.但它不起作用。 There are no errors or exceptions thrown, but the Data is not visible on the web service (the web service also has a GUI).没有抛出错误或异常,但数据在 web 服务上不可见(web 服务也有一个 GUI)。 I started to wonder if it is possible to send data to a web service in such a way or is it maybe necessary to create an xml file with a request and use HttpWebRequest?我开始怀疑是否可以通过这种方式将数据发送到 web 服务,或者是否有必要创建一个带有请求的 xml 文件并使用 HttpWebRequest?

Perhaps someone could help me figure out what is wrong, is there a way to check, what is going on during compilation?也许有人可以帮我找出问题所在,有没有办法检查编译过程中发生了什么? Unfortunately, I cannot include the URI or WSDL file, but hopefully my description of the issue will be sufficient.不幸的是,我不能包含 URI 或 WSDL 文件,但希望我对问题的描述就足够了。 Thanks in advance for any guidance!提前感谢您的任何指导!

Okay, with the suggestion from Robert Harvey (thank you once again) and some trial and error I managed to solve my problem.好的,根据 Robert Harvey 的建议(再次感谢您)和一些反复试验,我设法解决了我的问题。 Hopefully my answer will be useful to someone with the similar issue.希望我的回答对有类似问题的人有用。

I found out that in some cases WCF web service's calls are not visible in Fiddler.我发现在某些情况下 WCF web 服务的调用在 Fiddler 中不可见。 In my case it was due to some packages not being up-to-date.就我而言,这是由于某些软件包不是最新的。 These out-of-date packages were added automatically after connecting the web service via Microsoft WCF Web Service Reference Provider.这些过时的包是在通过 Microsoft WCF Web 服务参考提供程序连接 web 服务后自动添加的。

System.ServiceModel.Duplex/Http/NetTcp/Security all were installed in version 4.4 instead of 4.7. System.ServiceModel.Duplex/Http/NetTcp/Security 都安装在 4.4 版而不是 4.7 版中。 I updated them using Project->Manage NuGet Packages and now my requests are visible in Fiddler and I managed to verify that the way I used my method was correct, but the access to the web service (with the ability to save data) is currently limited, that is why the result was not visible directly on the web service (in the GUI).我使用 Project->Manage NuGet Packages 更新了它们,现在我的请求在 Fiddler 中可见,并且我设法验证我使用我的方法的方式是正确的,但是目前可以访问 web 服务(具有保存数据的能力)有限,这就是为什么结果在 web 服务(在 GUI 中)上不直接可见的原因。

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

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