ASP. NET reads HTML through URL (the foreground HTML is read in the background)

Source: Internet
Author: User

Today, I encountered a problem. The boss had to attach the result of the previously passed check boxes to the customer in an email. After communicating with the supervisor, it means to re-write a method in the background to concatenate a string and then output the content to the page. Generally, this method is quite reliable, however, the Code is too much and too bad. As a result, my head is too big and I really don't want to write the String concatenation work. Start to think about whether the output content is directly converted to img and then output. Finally, I didn't find any information. One article looked very reliable but was HTML5, so I didn't study it in depth. Therefore, I wonder if there is any way to directly read the HTML code generated by the front-end and output it directly in the email content. However, I am sorry, Google will find a question based on the keyword "asp.net get html: "How To Read Web Page HTML Code With ASP. NET? "(How to use ASP. NET to read webpage HTML code ?) It inspired me. Now that you can get HTML, the rest of the work will be solved!

As the time relationship between the following two articles is quite reliable, so I didn't have to read much. If you are interested, you can go and have a look... After all, the implementation methods and ideas are different!

How To Get HTML from a URL

How to Read the HTML of a Web Page Programmatically

The following is the HTML code and background code:

                     
BtnGetHTML_Click (WebClient MyWebClient = (txtURL. Text! = Namespace)
// Change ). UTF8Encoding oUTF8 =

When you encounter a tangle, an error occurs when you directly reference the intra-site address: "Too many attempts to redirect automatically. (Too effecautomatic redirections were attempted.) "No good solutions have been found yet.

For example, if you enter a Google address, there will be no problem, but reading HTML based on www.wnweixiu.com/gethtml.aspxunder the www.wnweixiu.com domain name will result in an error.

I saw many posts with related introductions, but I didn't have a reliable experience after the experiment. Then I looked for information from foreigners and found that the posts posted in disorder were actually foreign stuff.

I have posted two addresses for foreigners as follows. If you are interested, you can read them. I have not found a solution yet. The plan is aborted. Oh, my God, I don't want to splice strings.

Http://www.opendebug.com/article/422435

Http://www.rapidsnail.com/developer/topic/2007/105/3/46185/the-webclient-and-quot-try-to-be-automatic-redirection-of-too-many-times-and-quot-wrong.aspx

I really don't want to splice strings! I came to work this morning to try it out. I found an article and looked at it as original. It is indeed very reliable and must be supported. Solved the damn problem that has always plagued me: "Too many attempts to redirect automatically. "Problem, the original article is:" Too many automatic redirections were attempted solution ", you must read it, but what I need is to return data of the String type, his code only gives a stream, so he has to replace the stream with a string. However, this solution does not use the WebClient class for operations and uses the HttpWebRequest class. The following is the tested and optimized code, for everyone to learn:

                                      GetHTMLCode(=            request.CookieContainer = = ===              html =

Speaking of a little trick, mainly used to get a website domain name (such as a http://www.wnweixiu.com), if the local test that is http: // localhost + port number form (such as http: // localhost: 409 ).

Before that, I was always stupid enough to use the Request. the Url is then spliced and intercepted in a complex string, and no reliable GetLeftPart method is found under this attribute, Request. url. getLeftPart (UriPartial. authority) to obtain the domain name.

System. IO. Path. GetFileName (Request. PhysicalPath); the Page name can be obtained.

Request. ApplicationPath: Obtain the root path of the virtual application of ASP. NET applications on the server.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.