我有一个 WCF 服务(托管在 ASP.net 中),它基本上充当代理(将 http 转换为 https)。我需要连接到受信任的站点,获取图像,然后通过我的服务返回。
我想避免在开始将流发送给消费者之前在服务上下载整个图像,但我不完全确定如何去做。
我很确定我需要开始从受信任的站点获取响应流,并立即返回该流(希望 WCF 将在完成后处理流)。
到目前为止我有
[WebGet(UriTemplate = "/GetImage?imageUrl={imageUrl}")]
public Stream GetImage(string imageUrl)
{
if (string.IsNullOrWhiteSpace(imageUrl))
{ return new MemoryStream(Encoding.UTF8.GetBytes(ErrorBuilder.BuildJsonError("param"))); }
Uri verification = new Uri(imageUrl);
if (verification.Host != "flixster.com")
{
//TODO: Create new error for unknown urls.
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(ErrorBuilder.BuildJsonError("param")));
}
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageUrl);
//GetResponse() will get the whole thing, which I don't want.
//I just want to start getting bytes back and then ship the stream off to
//the consumer. Basically a proxy.
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
}
}
catch (Exception e)
{
ExceptionLogger.LogException(e);
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(ErrorBuilder.BuildJsonError("param")));
}
throw new NotImplementedException();
}
不确定我是否朝着正确的方向前进,任何帮助将不胜感激。谢谢大家!