English 中文(简体)
C# Webuser : Cannotload the pornography in html
原标题:C# Web Client : Cannot load the image in html when load the web page
  • 时间:2010-11-26 14:00:33
  •  标签:
  • c#

我正试图用c#(模拟 php功能——文档_get_contents)使用以下代码装载html的内容:

protected string file_get_contents(string fileName) 
{ 
  string sContents = string.Empty; 
  if (fileName.ToLower().IndexOf("http:") > -1) 
  { // URL 
   System.Net.WebClient wc = new System.Net.WebClient(); 
   byte[] response = wc.DownloadData(fileName); 
   sContents = System.Text.Encoding.ASCII.GetString(response); 
  } else { 
   // Regular Filename 
   System.IO.StreamReader sr = new System.IO.StreamReader(fileName); 
   sContents = sr.ReadToEnd(); 
   sr.Close(); 
  } 
 return sContents;
}

However, this does not load the image in the html when rendering the content. But when use PHP file_get_content, it does load the image in the html when rendering the content.

没有人会有什么想法?

最佳回答

Looking at the documentation for file_get_contents, there shouldn t be a difference between using a WebClient and this function to download the contents of an HTML page. In both cases, you ll get a string like this:

string html = @"<!DOCTYPE html>
<html>
<head>
   <title>Hello World</title>
</head>
<body>
   Image: <img src=""/image/test.png"">
</body>
</html>";

Of course, /image/test.png is relative to the URL you downloaded the HTML page from. So if you download the HTML page from http://www.example.com/test.html and render the retrieved data at http://www.yourdomain.net/foo/bar/qux.html the image tag will refer to http://www.yourdomain.net/image/test.png rather than http://www.example.com/image/test.png.

You need to rewrite relative URLs to absolute ones before rendering the HTML page. Have a look at the Html Agility Pack for this task.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签