C # determine whether the user uses the WeChat browser and display the real content or QR code accordingly,

Source: Internet
Author: User

C # determine whether a user uses a browser and display the actual content or QR code accordingly,

When we look at some webpages, we will find such a feature: some pages can only be accessed in, if accessed on a computer, only the QR code of the current address is displayed. How to implement this using C? Let's take a look at the code.

First, we need to determine which browser the user is using, which requires Request. the UserAgent attribute. The difference is discovered by capturing the UserAgent of a browser and a common browser. Below are the UserAgent of several common browsers:

Browser: Mozilla/5.0 (Linux; U; Android 4.4.2; zh-cn; 2013022 Build/HM2013022) AppleWebKit/533.1 (KHTML, like Gecko) version/4.0 MQQBrowser/5.4 TBS/025438 Mobile Safari/533.1 MicroMessenger/6.2.0.54 _ r00009949.561 NetType/WIFI Language/zh_CN

Firefox: Mozilla/5.0 (Windows NT 6.3; WOW64; rv: 38.0) Gecko/20100101 Firefox/38.0

IE11: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv: 11.0) like Gecko

Google Chrome: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36

 

Compared with the UserAgent of the above four browsers, we can find that the most important thing about the browser's UserAgent is to include the red MicroMessenger above, so we can determine whether the user has used the browser:

View Code

 
At this point, we can determine whether the user has used a browser. Next, we will start Step 2: generate a QR code when the user does not access the browser.

C # generate a QR code with a very useful plug-in:ThoughtWorks. QRCode. It is very simple to use. We download ThoughtWorks from the official website. QRCode. dll, reference it to our project, and then introduce ThoughtWorks to the page. QRCode. the Codec namespace can be used to generate a QR code:

  
View Code

 

There is a div on the page to display the QR code prompt.

View Code

Modify the browser judgment code above:

View Code

 

Here we are all done. Let's take a look at the different effects of browsing this page in a common browser and a browser:

Effect of accessing on a Common Browser

Scan access results

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.