使用绝对URL将图像渲染为响应 [英] Rendering image as response using absolute url
问题描述
我试图使用图片的绝对网址将图片作为网站的回复。我尝试下面的代码。响应正在显示,但图像不可见。我想这里发生了简单的错误。请帮帮我。
我的尝试:
im trying to get a image as response from a website using image's absolute url. i tried below code. response is showing but not the image is visible. i think here simple mistake is happened. please help me here.
What I have tried:
// Creates an HttpWebRequest with the specified URL.
HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("https://www.planwallpaper.com/static/images/HD-Wallpapers-C76_yCO55lB.jpg");
myWebRequest.Method = "GET";
myWebRequest.ContentType = "image/jpeg";
// Sends the HttpWebRequest and waits for the response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); //HttpWebRequest_GetResponse(): Returns a response from an Internet resource.
//get Stream Data from the response
Stream receiveStream = myHttpWebResponse.GetResponseStream();
//read the response from stream.
StreamReader streamReader = new StreamReader(receiveStream);
byte[] data = System.Text.Encoding.Default.GetBytes(streamReader.ReadToEnd());
Response.ContentType ="image/jpeg";
Response.BinaryWrite(data);
Response.End();
推荐答案
//get Stream Data from the response
Stream receiveStream = myHttpWebResponse.GetResponseStream();
//read the response from stream.
StreamReader streamReader = new StreamReader(receiveStream);
byte[] data = System.Text.Encoding.Default.GetBytes(streamReader.ReadToEnd());
You '从远程服务器读取二进制数据。您正在将二进制数据发送回客户端。那么为什么要将二进制数据转换为字符串(使用UTF8),然后将其转换回二进制(使用服务器的默认编码)?
删除该步骤,这可能会解决您的问题:
You're reading binary data from the remote server. You're sending binary data back to the client. So why are you converting that binary data to a string (using UTF8), and then converting it back to binary (using your server's default encoding)?
Remove that step, and that will probably fix your problem:
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Response.ContentType ="image/jpeg";
receiveStream.CopyTo(Response.OutputStream);
Response.End();
如果你想从url下载图像,那么最好的方法是使用
If You want to Download image from url then the best way is used
System.Net.WebClient
试试这个:
Try This:
Using System.Net.WebClient;
string imageUrl = "https://www.planwallpaper.com/static/images/HD-Wallpapers-C76_yCO55lB.jpg";
string SavePath = @"D:\Pictures\HD-Wallpapers-C76_yCO55lB.jpg";
System.Net.WebClient client = new WebClient();
client.DownloadFile(new Uri(imageUrl), SavePath);
好的然后试试这个:
Okay then try this:
public void save_file_from_url(string file_name, string url)
{
byte[] content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
content = br.ReadBytes(500000);
br.Close();
}
response.Close();
FileStream fs = new FileStream(file_name, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(content);
}
finally
{
fs.Close();
bw.Close();
}
}
这篇关于使用绝对URL将图像渲染为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!