我试图使用Ruby Sinatra和创建特定的网页一个简单的代理单页的代理。我能做到这一点在C#中,我似乎无法去解决它的末日,在C#代码如下:创建使用Ruby西纳特拉
<%@ WebHandler Language="C#" Class="Map" %>
using System;
using System.Web;
using System.Net;
using System.IO;
public class Map : IHttpHandler {
static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[0x1000];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, read);
}
public void ProcessRequest(HttpContext context)
{
string gmapUri = string.Format("http://maps.google.com/maps/api/staticmap{0}", context.Request.Url.Query);
WebRequest request = WebRequest.Create(gmapUri);
using (WebResponse response = request.GetResponse())
{
context.Response.ContentType = response.ContentType;
Stream responseStream = response.GetResponseStream();
CopyStream(responseStream, context.Response.OutputStream);
}
}
public bool IsReusable {
get {
return false;
}
}
}
红宝石西纳特拉的代码,我已经试过如下:
require 'rubygems'
require 'sinatra'
get '/mapsproxy/staticmap' do
request.path_info = 'http://maps.google.com/maps/api/staticmap'
pass
end
我假设西纳特拉一个不工作(获得404)如只将请求传递给页面在同一个域中。任何hep将不胜感激。
编辑:
随着铁皮人的帮助下,我想出了一个很好的解决方案简洁,这对我来说工作得很好:
get '/proxy/path' do
URI.parse(<URI> + request.query_string.gsub("|", "%7C")).read
end
感谢所有帮助。
干杯。这看起来很有希望......我现在只是在尝试一些东西...... – 2011-02-11 15:24:04
您需要添加一些异常处理,并且可能希望使用`Timeout`模块来提供更好的控制,以便在请求也被采用时长。请参阅http://stackoverflow.com/questions/4964044/using-open-uri-to-fetch-xml-and-the-best-practice-in-case-of-problems-with-a-remo/4966240#4966240举些例子。 – 2011-02-11 15:30:07