通过 WCF 服务中的 POST 使用 Web 服务

发布于 2024-11-26 15:09:20 字数 1813 浏览 0 评论 0原文

这就是我的情况。我必须使用另一个 WCF 服务中的第三方 Web 服务(不是 wcf),该服务将充当第一个服务和我的 Web 应用程序之间的中介。问题是我见过的几乎每个示例都需要您向应用程序添加 Web/服务引用才能生成代理,但我无法添加引用,它会返回错误,可能是由于需要进行某些身份验证。

该服务只能通过 GET 或 POST 来使用。我成功地通过网页中的 jquery ajax 调用中的 GET 和 POST 来使用该服务,但我不知道如何从 c# 中的 wcf 服务内部使用该服务。

来自服务的 GET 请求示例如下:

]

http://webservice.server.com/services/myservice?user=[username]&password=[password]&value1=[somevalue]&value2=[anothervalue 响应是一个 xml,其中包含操作的状态代码和状态消息,然后我将其保存到数据库中。

我该怎么做呢?

感谢您的帮助...

解决方案

感谢 Sean 为我指明了正确的方向。我是如何做到的:

参考文章:How to use HttpWebRequest to send POST request to another web server

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=" + username;
postData += ("&password=" + password);
postData += ("&value1=" + val1);
postData += ("&value2=" + val2);
byte[] data = encoding.GetBytes(postData);

// Prepare POST web request...
HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create(new Uri("http://webservice.server.com/services/myservice"));
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();

// Get response  
using (HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse)
{
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Read the whole contents and return as a string  
    result = reader.ReadToEnd();
}

XDocument doc = XDocument.Parse(result);

// Read XML

请如果您对我的解决方案有任何意见、反对或改进,欢迎提出所有意见。

So this is my situation. I have to consume a third party web service (not wcf) from another WCF service that will serve as an intermediary between the first service and my web app. The problem is almost every example I have seen on doing this requieres you to Add Web/Service Reference to the app in order to generate the proxy, but I can't add the reference, it returns an error, possibly due to some authentication required.

This service can be consumed only by either GET or POST. I was successful in consuming the service by both GET and POST from an ajax call with jquery in a web page, but I don't know how to consume the service from inside a wcf service in c#.

An example GET request from the service is:

http://webservice.server.com/services/myservice?user=[username]&password=[password]&value1=[somevalue]&value2=[anothervalue]

The response is an xml with the status code of the operation and a status message, which I then proceed to save to a database.

How might I go about doing this?

Thank you for any help...

SOLUTION

Thanks to Sean for pointing me in the right direction. How I did it:

Reference article: How to use HttpWebRequest to send POST request to another web server

ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "username=" + username;
postData += ("&password=" + password);
postData += ("&value1=" + val1);
postData += ("&value2=" + val2);
byte[] data = encoding.GetBytes(postData);

// Prepare POST web request...
HttpWebRequest myRequest =
  (HttpWebRequest)WebRequest.Create(new Uri("http://webservice.server.com/services/myservice"));
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();

// Get response  
using (HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse)
{
    // Get the response stream  
    StreamReader reader = new StreamReader(response.GetResponseStream());

    // Read the whole contents and return as a string  
    result = reader.ReadToEnd();
}

XDocument doc = XDocument.Parse(result);

// Read XML

Please if you have any comments on my solution, objections or improvements, all comments are welcomed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-12-03 15:09:20

如果您无法添加 Web 服务引用(我会进一步调查为什么您不能先这样做),恐怕您必须使用 WebClient 类 WebClient 或 Sean 建议的 HttpReqest 类

If you can't add the web service reference (I would investigate further why you can't do this first) I am afraid you'll have to do this manually issuing an HTTP Request manually using the WebClient class WebClient or the HttpReqest class as Sean suggests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文