1
我有一個WCF服務(託管在ASP.net中),它基本上充當代理(將http轉換爲https)。我需要連接到受信任的站點,獲取圖像,然後通過我的服務返回。如何在wcf(REST)中代理圖像流
我希望避免在開始將流發送給消費者之前必須下載服務上的整個圖像,但我不確定如何去做。
我很確定我需要開始從受信任的站點獲取響應流,並立即返回該流(希望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();
}
不知道如果我在正確的方向與否也得走,任何幫助,將不勝感激。謝謝大家!