我收到了客户的请求,要求为WMS服务的流量控制创建服务。这个想法是:
所以我需要某种代理将请求传递给目标WMS服务,同时记录传递的流量。
问题是我不知道如何开始以及从哪里开始。我会欣赏任何想法,概念等。
提前致谢!
答案 0 :(得分:2)
以下是我如何实现将请求传递给GeoServer WMS服务并返回地图图像的代理。我使用Web API,.NET 4.5。您可以使用日志记录和授权自定义我的示例。
public async Task<HttpResponseMessage> GetMapAsync()
{
HttpClient client = new HttpClient();
WmsLayer layer = _unitOfWork.LayerRepository.GetById(model.LayerId) as WmsLayer;
// This is typical WMS request url pointing to server behind the firewall,
// something like http://localhost:8081/geoserver/data/wms?service=WMS&version=1.1.0&request=GetMap&layers=data:layer1&styles=&bbox=337098.1084,4972868.3433,368833.6,5014800.3417&width=387&height=512&srs=EPSG:3765&format=image%2Fpng%3B+mode%3D8bit
string url = layer.GetWmsRequestUrl(layer.Url, model.SRS, model.Width, model.Height, model.BBox);
// See http://developer.greenbutton.com/downloading-large-files-with-the-net-httpclient/
var responseMessage = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
var response = Request.CreateResponse();
response.Content = new PushStreamContent(
async (outputStream, httpContent, transportContext) =>
{
var buffer = new byte[65536];
using (var httpStream = await responseMessage.Content.ReadAsStreamAsync())
{
var bytesRead = 1;
while ((bytesRead = httpStream.Read(buffer, 0, buffer.Length)) > 0)
{
await outputStream.WriteAsync(buffer, 0, bytesRead);
}
}
outputStream.Close();
}
, new MediaTypeHeaderValue("image/png"));
return response;
}