使用Web服务在c#中中继来自其他Web服务的调用(using a web service to relay calls to & from another web service in c#)

编程入门 行业动态 更新时间:2024-10-28 06:21:19
使用web服务在c#中中继来自其他Web服务的调用(using a web service to relay calls to & from another web service in c#)

我有一个第三方应用程序,需要Windows身份验证才能使用其Web服务。 我还有另一个需要使用这些Web服务的第三方应用程序,但它只能在调用Web服务时进行基本身份验证。

我必须创建一个网络服务,作为中间人来传递这两者之间的请求和响应。 有没有比在每次通话中阅读和重建肥皂信息更好/更简单的方法?

I have a 3rd party application that requires windows authentication to use its web services. I also have another 3rd party application that needs to consume these web services, however it is only capable of basic authentication when calling web services.

I have to create a web service that will act as the middleman that will relay the requests and responses between the two. Is there a better/easier way to do this than reading and reconstructing the soap messages on each call?

最满意答案

如果这个新的“代理”应用程序只执行这一项工作,那么我可能会使用Windows服务和HttpListener。 以下是一些让你开始的代码:

static class WebServer { private static readonly HttpListener Listener = new HttpListener { Prefixes = { "http://*/" } }; private static bool _keepGoing = true; private static Task _mainLoop; public static void StartWebServer() { if (_mainLoop != null && !_mainLoop.IsCompleted) return; _mainLoop = MainLoop(); } public static void StopWebServer() { _keepGoing = false; lock (Listener) { //Use a lock so we don't kill a request that's currently being processed Listener.Stop(); } try { _mainLoop.Wait(); } catch { /* je ne care pas */ } } private static async Task MainLoop() { Listener.Start(); while (_keepGoing) { try { var context = await Listener.GetContextAsync(); lock (Listener) { if (_keepGoing) ProcessRequest(context); } } catch (Exception e) { if (e is HttpListenerException) return; //this gets thrown when the listener is stopped //handle bad error here } } } private static void ProcessRequest(HttpListenerContext context) { string inputData; using (var body = context.Request.InputStream) { using (var reader = new StreamReader(body, context.Request.ContentEncoding)) { inputData = reader.ReadToEnd(); } } //inputData now has the raw data that was sent //if you need to see headers, they'll be in context.Request.Headers //now you can make the outbound request with authentication here //send result back to caller using context.Response } }

另一种选择是使用ASP.NET Web API和IIS,但这是一个很大的开销。 如果你已经在你想要的服务器上运行IIS,那么这是一个选项。 如果没有,我会去HttpListener路线。

If this new "proxy" application will only be doing this one job, then I'd maybe use a windows service and HttpListener. Here's some code to get you started:

static class WebServer { private static readonly HttpListener Listener = new HttpListener { Prefixes = { "http://*/" } }; private static bool _keepGoing = true; private static Task _mainLoop; public static void StartWebServer() { if (_mainLoop != null && !_mainLoop.IsCompleted) return; _mainLoop = MainLoop(); } public static void StopWebServer() { _keepGoing = false; lock (Listener) { //Use a lock so we don't kill a request that's currently being processed Listener.Stop(); } try { _mainLoop.Wait(); } catch { /* je ne care pas */ } } private static async Task MainLoop() { Listener.Start(); while (_keepGoing) { try { var context = await Listener.GetContextAsync(); lock (Listener) { if (_keepGoing) ProcessRequest(context); } } catch (Exception e) { if (e is HttpListenerException) return; //this gets thrown when the listener is stopped //handle bad error here } } } private static void ProcessRequest(HttpListenerContext context) { string inputData; using (var body = context.Request.InputStream) { using (var reader = new StreamReader(body, context.Request.ContentEncoding)) { inputData = reader.ReadToEnd(); } } //inputData now has the raw data that was sent //if you need to see headers, they'll be in context.Request.Headers //now you can make the outbound request with authentication here //send result back to caller using context.Response } }

The other option is you use ASP.NET Web API and IIS, but that's a lot of overhead. If you already have IIS running on the server you want this on, then that's an option. If not, I'd go the HttpListener route.

更多推荐

本文发布于:2023-07-31 09:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:web   Web   service   calls   relay

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!