Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to manipulate stream I'm reading before displaying on the page

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
0
0
US
I have the following, which reads an html file from a different server, then displays it on the page in a label.
Code:
protected void Page_Load(object sender, EventArgs e)
        {
            string thePartNo;
            thePartNo = Request.QueryString["part_no"];

            txtURL.Text = "[URL unfurl="true"]http://www.domainname.com/supplierstaging/123/"[/URL] + thePartNo.ToString() + "info.htm"; 
        }

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Create a WebRequest
            WebRequest wrRequest;
            wrRequest = WebRequest.Create(txtURL.Text);

            // Get the Response from the Request
            WebResponse wrResponse = wrRequest.GetResponse();
            
            // Read the Response into a stream and output the stream
            Stream objStream = wrResponse.GetResponseStream();
            StreamReader objStreamReader = new StreamReader(objStream);

            lblHTML.Text += objStreamReader.ReadToEnd();
        }

The html it returns includes an image tag with a relative src attribute, like: <img src="2343.jpg">

What I need to do is insert the local path into this src attribute so the image will display in the output... so it should end up like this: <img src="../whateverDirectory/2343.jpg">

Can anyone show me how this can be done? Is there a way to read/replace or something like that?

Thanks!
 
regex will probably be your best option in this situation. you can either do this on the server before send the text to the response stream, or you can use client code to alter the link once the HTML is rendered on the client.

server could would probably be the better option, if the client has JS disabled the client code would not alter the link.

alternatively you could alter your server directories so they align with the link(s) from the remote server. convention over configuration.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks for the quick responses. From what I gather, I can use a Regex or a Parser. Sounds like the Parser might be a better way to go. Either way, I don't really care. What I'm struggling with is how to implement it within my code.

I have tried adding a parser as follows, but it doesn't work. No errors, just nothing gets written to the screen.

Code:
protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Create a WebRequest
            WebRequest wrRequest;
            wrRequest = WebRequest.Create(txtURL.Text);

            // Get the Response from the Request
            WebResponse wrResponse = wrRequest.GetResponse();
            
            // Read the Response into a stream and output the stream
            Stream objStream = wrResponse.GetResponseStream();
            StreamReader objStreamReader = new StreamReader(objStream);
            
            while (!objStreamReader.EndOfStream)
            {
                string line = objStreamReader.ReadLine();
                line = line.Replace("<img border='0' src=", "<img border='0' src=/Parts/");
                
            }
            
            lblHTML.Text += objStreamReader.ReadToEnd();
            
        }

I think I'm missing how to integrate the newly replaced code back into the objStreamReader. Or something like that. Thoughts?
 
enabled tracing and/or step through the code to see what's happening.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Tried that. I get no errors or obvious issues. It seems to me that it just stops after this line:
line = line.Replace("<img border='0' src=", "<img border='0' src=/Parts/");

...which is why I think I'm missing something. Once I've replaced that '<img...' tag, is there something else that needs to happen before the objStreamReader is outputted?
 
oh, duh. line is declared for each line and then you replace it. you never do anything with line. after the loop you are at the end of the file and you tell the stream to read to the end of the file so text will always be emtpy (or null).
Code:
var reader = new StreamReader(objStream);
var text = reader.ReadToEnd().Replace("<img border='0' src=", "<img border='0' src=/Parts/");
lblHTML.Text += text;
you should also properly dispose of the stream when you are finished with them. google try/finally block or c# "using" keyword.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thanks Jason. That did it! I really appreciate your help. And I'll be sure to properly dispose of the stream as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top