我怎么能叫从C#与HTTP POST web服务 [英] How can I call a webservice from C# with HTTP POST

查看:205
本文介绍了我怎么能叫从C#与HTTP POST web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个C#类,将创建运行到www.temp.com一个Web服务连接,发送2串PARAMS的方法DoSomething的和得到字符串的结果。
我不希望使用WSDL。因为我知道web服务的参数,可以我只想做一个简单的电话。

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.

我想应该有一个容易和简单的方法来做到这一点在.net 2 ,但我找不到任何的例子...

I guess there should be an easy and simple way to do that in .Net 2, but I couldn't find any example...

推荐答案

如果这个Web服务是一个简单的HTTP GET,你可以使用的WebRequest

If this "webservice" is a simple HTTP GET, you can use WebRequest:

WebRequest request = WebRequest.Create("http://www.temp.com/?param1=x&param2=y");
request.Method="GET";
WebResponse response = request.GetResponse();



从那里,你可以看的 response.GetResponseStream 为输出。你可以打一个POST服务相同的方式。

From there you can look at response.GetResponseStream for the output. You can hit a POST service the same way.

但是,如果这是一个SOAP Web服务,这不是很容易。根据安全和Web服务的选项,有时你可以把已经形成的请求,并使用它作为一个模板 - 替换参数值,并把它(使用的WebRequest),然后手动解析SOAP响应...但在这种情况下,你看很多额外的工作中可能也只是使用 Wsdl.exe用生成代理。

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.

这篇关于我怎么能叫从C#与HTTP POST web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆