中继在asp.net请求(转发请求) [英] Relaying a request in asp.net (Forwarding a request)

查看:1060
本文介绍了中继在asp.net请求(转发请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的Web应用程序之间进行通信的Web应用程序(一个接收器和一个发送器,发送器与我的应用程序进行通信,而我的应用程序与两个通信)。

I have a web application that communicates between two different web applications (one receiver and one sender, the sender communicates with my application, and my application communicates with both).

一个经常的情况是,发送方发送一个HTTPRequest,我的应用程序,我接受它在一个HttpHandler。这反过来将HttpContext的一些businesslogic做一些水管。

A regular scenario is that the sender sends a HttpRequest to my application, and I receive it in an HttpHandler. This in turn sends the HttpContext to some businesslogic to do some plumbing.

我的商业课程完成存储数据之后(一些日志记录等),我想传达了同样的要求与所有的标头,形成到接收器的应用数据等。这必须从类中发送,而不是HttpHandler的

After my business classes are finished storing data (some logging etc), I want to relay the same request with all the headers, form data etc to the receiver application. This must be sent from the class, and not the HttpHandler.

真正的问题是 - 我怎么可以把一个HttpContext对象,并转发/只继电器完全相同的请求修改从 http://myserver.com/ URL来 HTTP:// receiver.com

The question is really - how can I take a HttpContext object, and forward/relay the exact same request only modifying the URL from http://myserver.com/ to http://receiver.com.

在任何最好的代码示例C#将是伟大的!

Any code examples in preferable c# would be great!

推荐答案

其实,像这样行之有效

HttpRequest original = context.Request;
HttpWebRequest newRequest = (HttpWebRequest)WebRequest.Create(newUrl);

newRequest .ContentType = original.ContentType;
newRequest .Method = original.HttpMethod;
newRequest .UserAgent = original.UserAgent;

byte[] originalStream = ReadToByteArray(original.InputStream, 1024);

Stream reqStream = newRequest .GetRequestStream();
reqStream.Write(originalStream, 0, originalStream.Length);
reqStream.Close();


newRequest .GetResponse();



编辑:ReadToByteArray方法只是使从流

edit: ReadToByteArray method just makes a byte array from the stream

这篇关于中继在asp.net请求(转发请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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