使用来自WCF服务的POST来使用Web服务 [英] Consume Web Service with POST from a WCF Service

查看:449
本文介绍了使用来自WCF服务的POST来使用Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的情况。我必须从另一个WCF服务中使用第三方Web服务(而不是wcf),该服务将充当第一个服务和我的Web应用程序之间的中介。问题几乎就是我所看到的每一个例子都要求你为了生成代理而添加Web /服务引用,但是我无法添加引用,它会返回错误,可能是由于需要一些身份验证。

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.

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

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#.

该服务的示例GET请求是:

An example GET request from the service is:

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

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

响应是一个xml,其中包含操作的状态代码和状态消息,然后我继续保存到数据库。

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.

我该怎么做呢?

感谢您的帮助...

解决方案

感谢Sean指出我正确的方向。我是怎么做到的:

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

参考文章:如何使用HttpWebRequest将POST请求发送到另一个Web服务器

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.

推荐答案

我想你想看一下HttpRequest类:

I think you'd want to take a look at the HttpRequest class:

http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx

这篇关于使用来自WCF服务的POST来使用Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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