SA Developer .NET

Welcome to SA Developer .NET Sign in | Join | Help
in Search

Transfer of files via port 80 from intranet to internet.

Last post 08-29-2008, 8:24 by JayKay4. 14 replies.
Sort Posts: Previous Next
  •  08-26-2008, 16:35 14315

    Transfer of files via port 80 from intranet to internet.

    Hi,

     I have a problem. I got a website on an intranet with an attachments folder. I also got a website on the internet outside the intranet. I need the website on the internet to access the attachments on the intranet. The security guys have allowed only port 80 to be open through the firewall from the internet website to the intranet website. I understand that if the end user had to view the attachments via a link to the intranet on the internet website, it will try and resolve the intranet domain name on the internet. Is there any way to go about doing this via port 80?

     

    Thanks,

    JK

  •  08-26-2008, 16:46 14316 in reply to 14315

    Re: Transfer of files via port 80 from intranet to internet.

    You can prob do a webrequest, get the data from the other side, save it locally.
  •  08-26-2008, 16:52 14317 in reply to 14316

    Re: Transfer of files via port 80 from intranet to internet.

    Is there another way besides downloading it, because there is going to be alot of files downloaded in that case.

  •  08-26-2008, 17:07 14318 in reply to 14317

    Re: Transfer of files via port 80 from intranet to internet.

    I would suggest creating a web service on the intranet that returned a list of the files in the folder, then on your internet site create a page that displays that list, and do web requests to stream the file in binary to the web browser when the user tries to download the file... I think that would work... Hmm

    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  08-27-2008, 10:13 14338 in reply to 14315

    Re: Transfer of files via port 80 from intranet to internet.

    We do exactly that with some images that are stored in a document management system. You can use whatever method you want to retrieve the binary data from the other server - we use a normal webservice. Once you have the data just set the content type of the page and write the binary data. This approach does have size limitations because you are essentially caching the object in server memory and not streaming it directly off the other server, but it should be good for normal size docs. Our images average 80kb and never exceed 250kb.

    Byte[] image = [download image data from other server];

    Response.ContentType="image/tiff";
    Response.Expires = 0;
    Response.Buffer = true;
    Response.Clear();
    Response.BinaryWrite(image);   
    Response.Flush();
    image = null; 

  •  08-27-2008, 15:30 14386 in reply to 14338

    Re: Transfer of files via port 80 from intranet to internet.

    Hi,

     I tried to the following code, but the image does not show up, please advise.

    Response.ContentType = "image/jpeg";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    http://10.1.5.24:80/WebTest/logo.jpg);
    NetworkCredential myCred = new NetworkCredential("username", "password", "intranet");
    CredentialCache myCache = new CredentialCache();
    myCache.Add(
    new Uri(@"http://10.1.5.24:80/WebTest/"), "Basic", myCred);
    //request.Credentials = myCache;
    request.Method = "GET";
    //request.ContentLength = 0;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream st = response.GetResponseStream();
    StreamReader sr = new StreamReader(st);
    string max = sr.ReadToEnd();
    Byte[] bytearry = System.Text.Encoding.UTF8.GetBytes(max);
    Response.ContentType =
    "image/jpeg";
    Response.BinaryWrite(bytearry);

  •  08-27-2008, 15:36 14387 in reply to 14386

    Re: Transfer of files via port 80 from intranet to internet.

    the above code, if used on a anonymous access control, loads the webpage with no image, just the placeholder (with a red x). If the code is run against a site with windows authentication and the correct username and password is used, it says

    The remote server returned an error: (401) Unauthorized.

  •  08-27-2008, 15:41 14388 in reply to 14386

    Re: Transfer of files via port 80 from intranet to internet.

    JayKay4:

    Hi,

     I tried to the following code, but the image does not show up, please advise.

    Response.ContentType = "image/jpeg";
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
    http://10.1.5.24:80/WebTest/logo.jpg);
    NetworkCredential myCred = new NetworkCredential("username", "password", "intranet");
    CredentialCache myCache = new CredentialCache();
    myCache.Add(
    new Uri(@"http://10.1.5.24:80/WebTest/"), "Basic", myCred);
    //request.Credentials = myCache;
    request.Method = "GET";
    //request.ContentLength = 0;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream st = response.GetResponseStream();
    StreamReader sr = new StreamReader(st);
    string max = sr.ReadToEnd();
    Byte[] bytearry = System.Text.Encoding.UTF8.GetBytes(max);
    Response.ContentType =
    "image/jpeg";
    Response.BinaryWrite(bytearry);

    1. Step through the code and check that the bytearry does have value.

    2. Make sure you put this in a totally separate page, I would call it showimage.aspx. In your code behind allow for the acceptance of the  WebRequest url (http://10.1.5.24:80/WebTest/logo.jpg) as parameter, then remove all the HTML from the page (leave the server tags)

     3. On another web page, add an image tag like so:

    <img src="showimage.aspx?imageUrl=http://10.1.5.24:80/WebTest/logo.jpg" />

    It works for me when I do it like that...


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  08-27-2008, 15:52 14389 in reply to 14388

    Re: Transfer of files via port 80 from intranet to internet.

    Also, try change this:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream st = response.GetResponseStream();
    StreamReader sr = new StreamReader(st);
    string max = sr.ReadToEnd();
    Byte[] bytearry = System.Text.Encoding.UTF8.GetBytes(max);

    to:

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    MemoryStream st = (MemoryStream)response.GetResponseStream();
    Byte[] bytearry = st.ToArray();


    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  08-27-2008, 16:35 14393 in reply to 14389

    Re: Transfer of files via port 80 from intranet to internet.

    The image is being streamed but does not show. If I stream a text file it shows. But alas jpeg does not, just shows the place holder. So is there a problem with the Response.ContentType.

  •  08-28-2008, 8:15 14397 in reply to 14393

    Re: Transfer of files via port 80 from intranet to internet.

    JayKay4:

    The image is being streamed but does not show. If I stream a text file it shows. But alas jpeg does not, just shows the place holder. So is there a problem with the Response.ContentType.

    If it's an image, I think it has to be within a <img> tag. So as per my above suggestion, put the page that writes out the content into another page inside an <img> tag...

    Heat_Rash:

    2. Make sure you put this in a totally separate page, I would call it showimage.aspx. In your code behind allow for the acceptance of the  WebRequest url (http://10.1.5.24:80/WebTest/logo.jpg) as parameter, then remove all the HTML from the page (leave the server tags)

     3. On another web page, add an image tag like so:

    <img src="showimage.aspx?imageUrl=http://10.1.5.24:80/WebTest/logo.jpg" />



    "I would love to change the world, but they won't give me the source code"
    Meeting Place - chat online with anyone, anytime, no downloads or plugins required
  •  08-28-2008, 10:41 14406 in reply to 14393

    Re: Transfer of files via port 80 from intranet to internet.

    You should read the response as a binary stream. If the mime type is set correctly you don't have to use an image tag to display the image.



    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(@"http://sadeveloper.net/Themes/default/images/common/watermarkcolumn.gif");
    req.Method = "GET";
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
    byte[] image = new byte[resp.ContentLength];
    Stream strm = resp.GetResponseStream();
    for (int i = 0; i < resp.ContentLength; i++) image[ i ] = (byte)strm.ReadByte();
    strm.Dispose();
    strm = null;
    resp.Close();
    resp = null;
    req = null;

    Response.Clear();
    Response.ContentType = "image/gif";
    Response.Expires = 0;
    Response.Buffer = true;
    Response.BinaryWrite(image);
    Response.Flush();
    image = null;

     

  •  08-28-2008, 14:12 14418 in reply to 14406

    Re: Transfer of files via port 80 from intranet to internet.

    Hi,

     Thanks for the great response. I have created a hack for the time being, until I can get the HTTP right. My temporary solution is creating a stored proc that reads a file from the intranet and sends it to the calling code, so for now no need for HTTP access, it uses the SQL port. Let me know what you guys think, like performance and other related issues I may face with this. Thanks again!

    PROC:

    ALTER PROCEDURE [dbo].[pr_COMMON_ImportTempFile]
    @Filename
    NVARCHAR(512)
    AS
    BEGIN
    DECLARE @Sql NVARCHAR(512)
    DECLARE @ID INT
    SET @Sql = 'INSERT INTO tb_COMMON_TempFiles (FileData, FileName) SELECT *, ''' + @Filename + ''' FROM OPENROWSET(BULK N''' + @Filename + ''', SINGLE_BLOB) AS Document'
    EXEC (@sql)
    SELECT FileData FROM tb_COMMON_TempFiles WHERE FileName = @Filename
    DELETE tb_COMMON_TempFiles WHERE FileName = @Filename
    END

    CODE:

     

    public static Stream GetImage(string filename)
    {
    using (SqlConnection connection = new SqlConnection("Data Source=server18;Initial Catalog=Test;User ID=test;Password = test;"))
    {
    using (SqlCommand command = new SqlCommand("pr_COMMON_ImportTempFile", connection))
    {
    command.CommandType =
    CommandType.StoredProcedure;
    command.Parameters.Add(
    new SqlParameter("@filename", filename));
    connection.Open();
    object result = command.ExecuteScalar();
    try
    {
    return new MemoryStream((byte[])result);
    }
    catch
    {
    return null;
    }
    }
    }
    }

    public void ProcessRequest()
    {
    //Set up the response settings
    Response.ContentType = "image/jpeg";
    Response.Cache.SetCacheability(
    HttpCacheability.Public);
    Response.BufferOutput =
    false;
    // Setup the PhotoID Parameter
    Int32 id = -1;
    Stream stream = null;
    //id = Convert.ToInt32(Request.QueryString["PhotoID"]);
    stream = GetImage(@"\\server18\Attachments\11345image.jpg");

    const int buffersize = 1024 * 16;
    byte[] buffer2 = new byte[buffersize];
    int count = stream.Read(buffer2, 0, buffersize);
    while (count > 0)
    {
    Response.OutputStream.Write(buffer2, 0, count);
    count = stream.Read(buffer2, 0, buffersize);
    }
    }

  •  08-28-2008, 21:26 14429 in reply to 14418

    Re: Transfer of files via port 80 from intranet to internet.

    Is there any piticular reason that don't you use a WCF service?

     I have pritty much the same setup as you have discribed, but I am hosting a WCF service on my intranet server.


    All things are possible

    http://www.codeproject.co.za
  •  08-29-2008, 8:24 14434 in reply to 14429

    Re: Transfer of files via port 80 from intranet to internet.

    I will need to create a better solution in the long run, but needed a quick fix for now with minimal code change and testing.
View as RSS news feed in XML
Powered by Community Server (Commercial Edition), by Telligent Systems