SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    [C#] Image of the remote site

    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 senderEventArgs 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;
            }
        }

    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Look at your HTML, specifically
    Code:
    <img alt="" src="<img alt="" src="http://www.meteowebcam.it/export_reg.php?reg=Toscana&day=0"/>"/></asp:Literal>
    What do you see that is wrong? You have an image tag within the src attribute of another image...

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank you for reply.

    I tried this but I have the same problem...
    I don't see the image of the remote site, but I see an icon undefined...
    PHP Code:
            <asp:Literal ID="Literal1" runat="server" Mode="passthrough">
            <
    img alt="" src="http://www.meteowebcam.it/export/toscana_1355547600_0.jpg"/>
            </
    asp:Literal
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,119
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    After the page loads, what is the final output? As your Page_Load is going to overwrite what is contained within the Literal. I'm curious as to what that may be.

  5. #5
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    After the page loads, what is the final output? As your Page_Load is going to overwrite what is contained within the Literal. I'm curious as to what that may be.
    thank you for help.
    as a matter a fact are authorized to use the image... []

    this is the solution:

    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>
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •