如何使用 HTTP POST 从 C# 调用 Web 服务
我想编写 c# 类,该类将创建与运行到 www.temp.com 的 Web 服务的连接,将 2 个字符串参数发送到方法 DoSomething 并获取字符串结果。 我不想使用 wsdl。因为我知道网络服务的参数,所以我只想进行一个简单的调用。
我想在 .Net 2 中应该有一个简单的方法来做到这一点,但我找不到任何例子......
I want to write a c# class that would create a connection to a webservice running to www.temp.com, send 2 string params to the method DoSomething and get the string result.
I don't want to use wsdl. Since I know the params of the webservice, I just want to make a simple call.
I guess there should be an easy and simple way to do that in .Net 2, but I couldn't find any example...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果此“webservice”是简单的 HTTP GET,则可以使用 WebRequest :
从那里您可以查看 response.GetResponseStream 用于输出。您可以用同样的方式访问 POST 服务。
然而,如果这是一个 SOAP Web 服务,那就没那么容易了。根据 Web 服务的安全性和选项,有时您可以采用已形成的请求并将其用作模板 - 替换参数值并发送它(使用 Webrequest),然后手动解析 SOAP 响应...但在这种情况下您正在考虑大量额外的工作,不妨使用 wsdl.exe 来生成代理。
If this "webservice" is a simple HTTP GET, you can use WebRequest:
From there you can look at response.GetResponseStream for the output. You can hit a POST service the same way.
However, if this is a SOAP webservice, it's not quite that easy. Depending on the security and options of the webservice, sometimes you can take an already formed request and use it as a template - replace the param values and send it (using webrequest), then parse the SOAP response manually... but in that case you're looking at lots of extra work an may as well just use
wsdl.exe
to generate proxies.我将探索如何将 ASP.NET MVC 用于您的 Web 服务。您可以通过标准表单参数提供参数并以 JSON 形式返回结果。
HttpWebRequest
在您的客户端中,使用您拥有的
I would explore using ASP.NET MVC for your web service. You can provide parameters via the standard form parameters and return the result as JSON.
In your client, use the HttpWebRequest
where you have
另一种调用 POST 方法的方法是,我曾经在 WebAPI 中调用 POST 方法。
One other way to call POST method, I used to call POST method in WebAPI.
您可以使用 Newtonsoft.Json 返回 List 对象:
You can return List object use Newtonsoft.Json:
这是一个无需凭据即可调用任何服务的静态方法
这是一个可使用凭据来调用任何服务的静态方法
This a static method to call any service without credentials
This a static method to call any service with credentials