WebRequest for images

  • Thread starter Thread starter TomislaW
  • Start date Start date
T

TomislaW

I need to include some pictures from other web application (written in
Java), Source of pictures are: <http://www.xy.com/bla/id/imageNumber>. I
don't know how many pictures there are So I need to read header from
WebRequest to see if it is default image or not This is my code:

int i = 0;
bool defaultImage = true;
do
{
i++;
WebRequest myWebRequest = WebRequest.Create("http://www.xy.com/bla/id/" +
i.ToString());
WebResponse myWebResponse = myWebRequest.GetResponse();
defaultImage = bool.Parse(myWebResponse.Headers["DEFAULTIMAGEUSED"]);
myWebResponse.Close();
if(!defaultImage)
Response.Write("<img src='" + myWebResponse.ResponseUri.AbsoluteUri +"'>");
}while(!defaultImage);

this is very slow and I think that my web application doing two requests for
one picture (
once when I make WebRequest in code and once when I set src of img tag in
html)

, is there any better solution?
 
Tomisla,

Here is an excerpt from an image scraper I wrote. I parse the html returned
from a web request with a regular expression to get every image on the page.

If Not Html Is Nothing Then

'---Create a regular expression to find all image uris
Dim RegEx As New
System.Text.RegularExpressions.Regex("(?:""|')([^""']+.(gif|jpg|jpeg|jpe|bmp|png|tif|tiff|swf))(?:""|')")

Dim Matches As System.Text.RegularExpressions.MatchCollection =
RegEx.Matches(Html)

'---Store the image uris found on the page in the ImageUriArrayList
For Each UriMatch As System.Text.RegularExpressions.Match In Matches

CurrentLink = UriMatch.ToString.Replace(""""c, "").Replace("'"c, "")

CurrentLink = CreateFullUri(ResponseUri.AbsoluteUri, CurrentLink)

If Not ImageUriArrayList.Contains(CurrentLink) Then

ImageUriArrayList.Add(CurrentLink)

'---Keep the page search from searching the image
LinksFound.Add(CurrentLink)

'---Up the image count to display
ImagesFoundCount += 1

End If

Next



End If

Each image link found is placed into an array of links and then a single
request for each image is made.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
thank you, but images are not on the same page(link)
image number one: http://www.xy.com/bla/id/1
image number two http://www.xy.com/bla/id/2
....
image number n: http://www.xy.com/bla/id/n

for one object id i don't know how much images(links) I have



S. Justin Gengo said:
Tomisla,

Here is an excerpt from an image scraper I wrote. I parse the html
returned from a web request with a regular expression to get every image
on the page.

If Not Html Is Nothing Then

'---Create a regular expression to find all image uris
Dim RegEx As New
System.Text.RegularExpressions.Regex("(?:""|')([^""']+.(gif|jpg|jpeg|jpe|bmp|png|tif|tiff|swf))(?:""|')")

Dim Matches As System.Text.RegularExpressions.MatchCollection =
RegEx.Matches(Html)

'---Store the image uris found on the page in the ImageUriArrayList
For Each UriMatch As System.Text.RegularExpressions.Match In Matches

CurrentLink = UriMatch.ToString.Replace(""""c, "").Replace("'"c, "")

CurrentLink = CreateFullUri(ResponseUri.AbsoluteUri, CurrentLink)

If Not ImageUriArrayList.Contains(CurrentLink) Then

ImageUriArrayList.Add(CurrentLink)

'---Keep the page search from searching the image
LinksFound.Add(CurrentLink)

'---Up the image count to display
ImagesFoundCount += 1

End If

Next



End If

Each image link found is placed into an array of links and then a single
request for each image is made.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
TomislaW said:
I need to include some pictures from other web application (written in
Java), Source of pictures are: <http://www.xy.com/bla/id/imageNumber>. I
don't know how many pictures there are So I need to read header from
WebRequest to see if it is default image or not This is my code:

int i = 0;
bool defaultImage = true;
do
{
i++;
WebRequest myWebRequest = WebRequest.Create("http://www.xy.com/bla/id/" +
i.ToString());
WebResponse myWebResponse = myWebRequest.GetResponse();
defaultImage = bool.Parse(myWebResponse.Headers["DEFAULTIMAGEUSED"]);
myWebResponse.Close();
if(!defaultImage)
Response.Write("<img src='" + myWebResponse.ResponseUri.AbsoluteUri
+"'>");
}while(!defaultImage);

this is very slow and I think that my web application doing two requests
for one picture (
once when I make WebRequest in code and once when I set src of img tag in
html)

, is there any better solution?
 
Tomisla,

Then a Do While may be in order how about something like this?

'---Get the response produced by the request
Dim Response As System.Net.HttpWebResponse

Response = CType(ImageWebRequest.GetResponse, System.Net.HttpWebResponse)

While Response.StatusCode = System.Net.HttpStatusCode.OK
'---Load the image

'---Get the next repsonse
Response = CType(ImageWebRequest.GetResponse,
System.Net.HttpWebResponse)
End While

'---Clean up
Response.Close()
Response = Nothing


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
TomislaW said:
thank you, but images are not on the same page(link)
image number one: http://www.xy.com/bla/id/1
image number two http://www.xy.com/bla/id/2
...
image number n: http://www.xy.com/bla/id/n

for one object id i don't know how much images(links) I have



S. Justin Gengo said:
Tomisla,

Here is an excerpt from an image scraper I wrote. I parse the html
returned from a web request with a regular expression to get every image
on the page.

If Not Html Is Nothing Then

'---Create a regular expression to find all image uris
Dim RegEx As New
System.Text.RegularExpressions.Regex("(?:""|')([^""']+.(gif|jpg|jpeg|jpe|bmp|png|tif|tiff|swf))(?:""|')")

Dim Matches As System.Text.RegularExpressions.MatchCollection =
RegEx.Matches(Html)

'---Store the image uris found on the page in the ImageUriArrayList
For Each UriMatch As System.Text.RegularExpressions.Match In Matches

CurrentLink = UriMatch.ToString.Replace(""""c, "").Replace("'"c, "")

CurrentLink = CreateFullUri(ResponseUri.AbsoluteUri, CurrentLink)

If Not ImageUriArrayList.Contains(CurrentLink) Then

ImageUriArrayList.Add(CurrentLink)

'---Keep the page search from searching the image
LinksFound.Add(CurrentLink)

'---Up the image count to display
ImagesFoundCount += 1

End If

Next



End If

Each image link found is placed into an array of links and then a single
request for each image is made.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
TomislaW said:
I need to include some pictures from other web application (written in
Java), Source of pictures are: <http://www.xy.com/bla/id/imageNumber>. I
don't know how many pictures there are So I need to read header from
WebRequest to see if it is default image or not This is my code:

int i = 0;
bool defaultImage = true;
do
{
i++;
WebRequest myWebRequest = WebRequest.Create("http://www.xy.com/bla/id/"
+ i.ToString());
WebResponse myWebResponse = myWebRequest.GetResponse();
defaultImage = bool.Parse(myWebResponse.Headers["DEFAULTIMAGEUSED"]);
myWebResponse.Close();
if(!defaultImage)
Response.Write("<img src='" + myWebResponse.ResponseUri.AbsoluteUri
+"'>");
}while(!defaultImage);

this is very slow and I think that my web application doing two requests
for one picture (
once when I make WebRequest in code and once when I set src of img tag
in html)

, is there any better solution?
 
try a HEAD request instead of a GET. this will return the headers and not
the image data, so the request should be faster. if it doesn't work, report
a bug to your java group, should be trival to fix.

-- bruce (sqlwork.com)
 
Bruce Barker said:
try a HEAD request instead of a GET. this will return the headers and not
the image data, so the request should be faster. if it doesn't work,
report a bug to your java group, should be trival to fix.

-- bruce (sqlwork.com)

how to do this with c#?
I don't work with Java, my partner site is in Java
 
TomislaW said:
how to do this with c#?
I don't work with Java, my partner site is in Java

I guess Bruce meant the case that the Java web application doesn't
support HEAD requests, which is very unlikely. All you need to in your
code is to set WebRequest.Method to "HEAD".

Cheers,
 
Joerg Jooss said:
I guess Bruce meant the case that the Java web application doesn't
support HEAD requests, which is very unlikely. All you need to in your
code is to set WebRequest.Method to "HEAD".

Thanks

That was answer I was looking for
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top