 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

December 15th, 2012, 04:23 PM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
Image of the remote site
Hello guys!
I need your help.
This is code of my net page (C#).
I don't have error but in the output of webpage I don't see the image of the remote site, but I see an icon undefined...
How can i do it?
Can someone test my code?
please
Any help?
Thank you.
PHP Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CallRemote_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server" Mode="passthrough">
<img alt="" src="<img alt="" src="http://www.meteowebcam.it/export_reg.php?reg=Toscana&day=0"/>"/></asp:Literal>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
public partial class CallRemote_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = getHtml("http://www.meteowebcam.it/export_reg.php?reg=Toscana&day=0");
}
public string getHtml(string url)
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
return result;
}
catch (Exception exception)
{
return "The following error occurred: " + exception;
}
}
}
|
|

December 16th, 2012, 06:27 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
The HTML you're retrieving from the remote server contains a relative link to the image (e.g. (../../export/toscana_1355612400_0.jpg). Since you're displaying the HTML in your page directly, it means the browser will search for the image toscana_1355612400_0.jpg in the export folder on *your* server.
I see a few alternatives:
1. Embed the external HTML in an iframe. This way, the request is made to the remote server and the image will be found.
2. Grab the image directly instead of the surrounding HTML
3. Search and replace the relative path on the remote server with a full path.
All of this assumes that reusing the remote image is legal, of course....
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

December 16th, 2012, 06:51 AM
|
|
Authorized User
|
|
Join Date: Aug 2012
Posts: 27
Thanks: 6
Thanked 0 Times in 0 Posts
|
|
thank you for help.
as a matter a fact are authorized to use the image...
this is the solution:
Quote:
Originally Posted by Imar
2. Grab the image directly instead of the surrounding HTML
|
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Drawing;
public partial class CallRemote_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://www.meteowebcam.it/export/toscana_1355612400_0.jpg";
string file_name = Server.MapPath(".") + "\\toscana_1355612400_0.jpg";
save_file_from_url(file_name, url);
}
public string getHtml(string url)
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
return result;
}
catch (Exception exception)
{
return "The following error occurred: " + exception;
}
}
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();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CallRemote_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="http://www.meteowebcam.it/meteo/Toscana.html">
<asp:Image ID="Image1" runat="server" ImageUrl="/WebApplication1/CallRemote/toscana_1355612400_0.jpg" /></a>
</div>
</form>
</body>
</html>
Last edited by cms9651; December 16th, 2012 at 07:40 AM..
|
|

December 16th, 2012, 07:42 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can't retrieve an image, convert it to text and then display it in a label. Instead, you need to either:
a) Directly link to the image. Just point the ImageUrl of the a server side image to the URL of the remote image.
b) Link an asp:Image to a custom handler (such as an ASHX file) that retrieves the remote image and then streams it back to the client as an image. E.g.:
<asp:Image .... ImageUrl="GetRemoteImage.ashx?ID=123" .... />
Then GetRemoteImage.ashx would retrieve the image and stream it back. Take a look here for more details: http://imar.spaanjaars.com/414/stori...with-aspnet-20
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

January 4th, 2013, 10:49 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I met the same problem and fixed it. Thanks very much for your discussion.
|
|
 |