【发布时间】:2011-07-28 02:48:36
【问题描述】:
我希望能够将路径映射到不同的域,但将原始地址保留在地址栏中。有没有办法对此进行编码,或者是否在 IIS 中。如果可能的话,我最好想要一个编码方法。
我有一个链接,其 href 类似于“http://www.example.com/invproxy.aspx?id=1&batch=1”。 我想将该映射映射到“http://www.otherdomain.com/Files/TheFileRequest.aspx”。
我使用原始请求的查询字符串生成映射到的路径。它适用于 Server.Transfer 或 Response.Redirect 但我希望地址栏仍然显示最初请求的 URL。这可能吗?
谢谢
编辑:我已经解决了这个问题(可能不是最经济的方法),但下面是我在 invproxy.aspx 的 Page_load 事件中使用的代码
// Just test file, no code to select path yet.
string requestPath = "http://www.otherdomain.com/Files/TestFile.pdf";
// Setup the request for the PDF File
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(requestPath);
// Get the response from otherdomain.com
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Assign the invproxy response the PDF ContentType
Response.ContentType = "application/pdf";
// Get the body of the response in a stream object
Stream s = resp.GetResponseStream();
// Create a byte array to be read into from the stream
byte[] buffer = new byte[resp.ContentLength];
int position = 0;
while (true)
{
//Read bytes one by one, slow but causes errors trying to read the whole thing.
//I need to use chunks here.
int b = s.ReadByte();
if (b == -1)
{
//This is the end of the stream
break;
}
else
{
//Set the byte at the current position to the value just read
buffer[position] = (byte)b;
//Advance the position by one.
position++;
}
}
//breakpoint debugging
string sa = Encoding.Default.GetString(buffer);
//Write the file to the invproxy response.
Response.BinaryWrite(buffer);
编辑:为了添加完成的结果,我的 PDF 文档显示在浏览器的新选项卡中(如果兼容),地址栏显示为 http://www.example.com/invproxy?myquerystring
【问题讨论】:
-
otherdomain.com 是否存在于同一台服务器上,或者您是否控制它?
-
是的,它们都在我的 VPS 上运行,我控制它们。
-
我可能没有 100% 完全理解您要做什么,但该技术应该为您提供当前 URL 的部分