Find whether a dynamically called url has image in it - c#

I have a products sale module in which products are uploaded from cj and saved in to database..today i noticed few records contained image url but returns 404(eg image url:http://www.bridalfashionmall.com/images/satin-2.jpg) hence shows no image in the repeater ..how can i check whether the url called dynamically has image in it

The method suggested by sean could be used as first pass. As second pass you can try loading the stream into image and see if it is actually image?
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(imageFilePath);
request.Timeout = 5000;
request.ReadWriteTimeout = 20000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Drawing.Image img = System.Drawing.Image.FromStream(response.GetResponseStream());
// Save the response to the output stream
Response.ContentType = "image/gif";

this one helped---
http://stackoverflow.com/questions/1639878/how-can-i-check-if-an-image-exists-at-http-someurl-myimage-jpg-in-c-asp-net
this one too worked
try
{
WebClient client = new WebClient();
client.DownloadData(ImageUrl);
}
catch
{
imgPhoto.ImageUrl = ../User/Images/ResourceImages/Candychocolate1.jpg";//default image path
}

Related

Error 403 while trying to download an image, but not to show it

I'm getting an error 403 while I try to do anything to an image's Url (be it get the file size or download it) but I don't get any error while trying to show the image.
I hope I'm clear enough, but if need be this is an example of url posing problem:
Image URL / Site show the image
I'm using this code to get the file size which works great but not on this site for exemple :
public void getFileSize(string uri)
{
try
{
waitGetSize = 0;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Timeout = 5000;
req.Method = "HEAD";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
imgSize = resp.ContentLength;
imgSizeKb = imgSize / 1024;
waitGetSize = 1;
}
catch (Exception ex)
{
MetroMessageBox.Show(this, ex.Message, "Exception :", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
As pointed out by cFrozenDeath, I used a HEAD request, so I tried using a GET request to the exact same effect. Same result by simply not stating the request type I want.
So is there a way to get the file size or at least download the file knowing it's shown OK when opened in a browser?
You have to mimic a webbrowser when you want to scrape content from websites.
Sometimes this means you need to provide and/or keep the Cookies you get when you land initially on a website, sometimes you have to tell the webserver which page linked to the resource.
In this case you need to provide the Referer in the header:
public void getFileSize(string uri)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
// which page do we want that server to believe we call this from
req.Referer = "http://www.webtoons.com/";
req.Timeout = 5000;
req.Method = "GET"; // or do a HEAD
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// rest omitted
}
That particular image has a length of 273073 bytes.
Do note that scraping content might be against the terms of service of the particular website. Make sure you don't end up doing illegal stuff.

PictureBox.Load Method add picture from website that requires authentication

I would like to add a picture to a PictureBox via the .Load() Method. The Problem with that picture is that it stays on a website which requires authentication!
Link is like: https://intranet.company.com/_layouts/15/company/PortraitHandler.ashx?isinternal=true&account=test/account
How can I fix this?
Solved it like this:
public Bitmap getImageFromURL(string sURL)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(sURL);
Request.Method = "GET";
Request.UseDefaultCredentials = true;
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Response.GetResponseStream());
Response.Close();
return bmp;
}

outputting image with http handler

I have the following code for my handler I've debugged it and I can see that my image variable b has the actual image I need however I am not able to display it in my browser. When I run this I just get System.Drawing.Bitmap on the screen instead of the image. I am not sure how to write it to the browser. Any ideas would be much appreciated it thanks.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myaddress");
request.Credentials = new NetworkCredential("username", "password");
request.Method = "GET";
request.Accept = "image/jpeg";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream s = response.GetResponseStream();
System.Drawing.Image b = System.Drawing.Image.FromStream(s);
context.Response.ContentType = "image/jpeg";
context.Response.Write(b);
Write it to the output stream:
b.Save(context.Response.OutputStream, ImageFormat.Jpeg);

Retrieving website content and returning it in ASP.NET MVC 4

I have two servers. One is a private server and I don't want users to have direct access to it, and the other one is the server that public does have access to.
I can access my private server by URL like: http://xxx.xx.xxx.xxx/
What i want to do is create some kind of "proxy", only to work with my private server. My idea is to go to: http://www.domain.com/server/path/here/something
This page should show me the content of http://xxx.xx.xxx.xxx/path/here/something
I have this working, but the only way I could make it work was to return the content as a string, and then the browser would interpret the HTML.
This works fine for pages that return HTML content, but it doesn't work (of course) if I want to access a .gif or any kind of file directly.
Here's the code I currently have:
public string Index(string url)
{
string uri = "http://xxx.xx.xxx.xxx/" + url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
string resultado = responseStream.ReadToEnd();
return resultado;
}
How can I change my code so that it works for any file ?
You can check the response content type and do what you need based on that.
You'll need to change your action to return ActionResult instead of string.
if(response.ContentType.Equals("text/html"))
{
//show html stuff
return Content(resultado);
}
else if(response.ContentType.Contains("image/"))
{
var ms = new MemoryStream();
responseStream.BaseStream.CopyTo(ms);
var imageBytes = ms.ToArray();
return File(imageBytes, response.ContentType);
}
you have to write a system which reads your html or images from resultado and do something according to that PLUS you need to control your Url as well.

Not generating a complete response from a HttpWebResponse object in C#

I am creating a HttpWebRequest object from another aspx page to save the response stream to my data store. The Url I am using to create the HttpWebRequest object has querystring to render the correct output. When I browse to the page using any old browser it renders correctly. When I try to retrieve the output stream using the HttpWebResponse.GetResponseStream() it renders my built in error check.
Why would it render correctly in the browser, but not using the HttpWebRequest and HttpWebResponse objects?
Here is the source code:
Code behind of target page:
protected void PageLoad(object sender, EventsArgs e)
{
string output = string.Empty;
if(Request.Querystring["a"] != null)
{
//generate output
output = "The query string value is " + Request.QueryString["a"].ToString();
}
else
{
//generate message indicating the query string variable is missing
output = "The query string value was not found";
}
Response.Write(output);
}
Code behind of page creating HttpWebRequest object
string url = "http://www.mysite.com/mypage.aspx?a=1";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url)
//this if statement was missing from original example
if(User.Length > 0)
{
request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");
request.PreAuthenticate = true;
}
request.UserAgent = Request.UserAgent;
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream resStream = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(resStream, encode, true, 2000);
int count = readStream.Read(read, 0, read.Length);
string str = Server.HtmlEncode(" ");
while (count > 0)
{
// Dumps the 256 characters on a string and displays the string to the console.
string strRead = new string(read, 0, count);
str = str.Replace(str, str + Server.HtmlEncode(strRead.ToString()));
count = readStream.Read(read, 0, 256);
}
// return what was found
result = str.ToString();
resStream.Close();
readStream.Close();
Update
#David McEwing - I am creating the HttpWebRequest with the full page name. The page is still generating the error output. I updated the code sample of the target page to demonstrate exactly what I am doing.
#Chris Lively - I am not redirecting to an error page, I generate a message indicating the query string value was not found. I updated the source code example.
Update 1:
I tried using Fiddler to trace the HttpWebRequest and it did not show up in the Web Sessions history window. Am I missing something in my source code to get a complete web request and response.
Update 2:
I did not include the following section of code in my example and it was culprit causing the issue. I was setting the Credentials property of the HttpWebRequest with a sevice account instead of my AD account which was causing the issue.
I updated my source code example
What webserver are you using? I can remember at one point in my past when doing something with IIS there was an issue where the redirect between http://example.com/ and http://example.com/default.asp dropped the query string.
Perhaps run Fiddler (or a protocol sniffer) and see if there is something happening that you aren't expecting.
Also check if passing in the full page name works. If it does the above is almost certainly the problem.
Optionally, you can try to use the AllowAutoRedirect property of the HttpRequestObject.
I need to replace the following line of code:
request.Credentials = new NetworkCredentials("myaccount", "mypassword", "mydomain");
with:
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

Categories

Resources