WeChat official account development series-Http request encapsulation base class, public-http

Source: Internet
Author: User

Public Account Development Series-Http request encapsulation base class, public-http

HttpHelper requests encapsulate the base class and support get and POS requests. This facilitates interface interaction and prepares for subsequent interface interaction.

1. HttpHelper help base class

[Csharp]View plaincopy
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. IO;
  4. Using System. Linq;
  5. Using System. Text;
  6. Using System. Net;
  7. Using System. Net. Security;
  8. Namespace Weixin. Utils
  9. {
  10. Public class HttpHelper
  11. {
  12. Public static string Post (string url, string paramData)
  13. {
  14. Return Post (url, paramData, Encoding. UTF8 );
  15. }
  16. Public static string Post (string url, string paramData, Encoding encoding)
  17. {
  18. String result;
  19. If (url. ToLower (). IndexOf ("https", System. StringComparison. Ordinal)>-1)
  20. {
  21. ServicePointManager. ServerCertificateValidationCallback =
  22. New RemoteCertificateValidationCallback (sender, certificate, chain, errors) =>{ return true ;});
  23. }
  24. Try
  25. {
  26. Var wc = new WebClient ();
  27. If (string. IsNullOrEmpty (wc. Headers ["Content-Type"])
  28. Wc. Headers. Add ("Content-Type", "application/x-www-form-urlencoded ");
  29. Wc. Encoding = encoding;
  30. Result = wc. UploadString (url, "POST", paramData );
  31. }
  32. Catch (Exception e)
  33. {
  34. Throw;
  35. }
  36. Return result;
  37. }
  38. Public static string Get (string url)
  39. {
  40. Return Get (url, Encoding. UTF8 );
  41. }
  42. Public static string Get (string url, Encoding encoding)
  43. {
  44. Try
  45. {
  46. Var wc = new WebClient {Encoding = encoding };
  47. Var readStream = wc. OpenRead (url );
  48. Using (var sr = new StreamReader (readStream, encoding ))
  49. {
  50. Var result = sr. ReadToEnd ();
  51. Return result;
  52. }
  53. }
  54. Catch (Exception e)
  55. {
  56. Throw e;
  57. }
  58. }
  59. }
  60. }

2. Help class usage

[Csharp]View plaincopy
  1. Var data = "{\" touser \ ": \" Rich \"},";
  2. Data + = "{\" msgtype \ ": \" text \"},";
  3. Data + = "{\" text \ ": [{\" content \ ": \" test \ "}]}";
  4. Var json = HttpHelper. Post ("https://api.weixin.qq.com/cgi-bin/message/custom/send? Access_token = "+ accessToken, data );
[Csharp]View plaincopy
  1. Var result = JsonHelper. Deserialize (json );

Several steps to become a public platform developer

1. Set the reply message when the public account is followed.
2. Automatic Message reply settings.
3. Automatic keyword reply.
What do you mean by these three points? Let's give a brief introduction.
1. A message replied by the public account when a common user pays attention to the public account. The content can be set to welcome information.
2. When a user sends a message to a public account, the public account automatically replies to the message. What does it mean? Do you understand the automatic reply of QQ exit status?
3. the Keyword Automatic reply means that when the public account receives some messages sent by the user, such
Hello. The public account can also set full match or cancel full match for the user's reply content.
Then, we will introduce you to the developer mode of the public account. The developer mode is to develop your own reply program through the interface of the public platform.
The developer mode can basically complete any of the above three features, and can be more perfect and user-friendly.

After the development mode is enabled, you need to set a Token value to verify the interface URL and download the Development sdk of the public platform. Find
Define ("TOKEN", "weixin ");
[Color = #336699! Important] copy the code
The following weixin is the Token value, and the content is arbitrary. Go to the public platform development mode and set the URL and Token value. The Token value must be consistent with the one set in the PHP file, the URL address is the interface address of your API.
After the verification is submitted. You can perform a simple test.
Open the interface file and you can see that a class has been written in the sdk. Several Functions encapsulated in the class will be introduced to you.
Valid is mainly used to verify the interface. It seems to be useless in other cases. I have not found it yet.
ResponseMsg is the most widely used function to reply to messages.
CheckSignature, isn't it necessary? As the name suggests, it is used for verification.
Find
$ WechatObj-> valid ();
[Color = #336699! Important] copy the code
Change
$ WechatObj-> responseMsg ();
[Color = #336699! Important] copy the code
Now you can use the automatic reply function of the interface. What about the above content? If you follow the procedure described above, and now you send a message to the public account, a welcome message will be returned.
To wechat world! Why? Please continue reading ..
The interface principle is that when a user sends a message to a public account, the server will check whether you have enabled some functions of the Development Platform. If the development mode is enabled, it will send a piece of xml in your development mode. The content of this xml is actually the message sent by the user. After processing by the program, the server returns an xml message to the interface of the public platform, it is also the content replied to the user, of course, to ensure the efficiency of your program, if the public platform interface does not receive the returned xml within five seconds, it will give up. Let's take a look at the responseMsg function.
Public function responseMsg ()
{
// Get post data, May be due to the different environments
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
// Extract post data
If (! Empty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement ',
LIBXML_NOCDATA );
$ FromUserna ...... remaining full text>

In the developer mode of the aspnet public platform, how does one automatically reply to messages after adding attention? For details about sending codes, refer

We recommend that you use a third-party SDK for Asp. Net: WeiXinMPSDK
This SDK encapsulates common functions. You can try it.
 

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.