WeChat Public Account Development tutorial 6th-Content Length limitations for text messages _php Tutorial

Source: Internet
Author: User
I believe many friends have encountered this problem: when the text message sent is too long, will not do any response. So what is the maximum length of text messages allowed? How do we calculate the length of the text? Why are some people reacting as if the maximum length of text messages supported is more than 1300? This article will completely dismiss everyone's doubts.

The message length is limited to 2048 in the interface document

As you can see, the interface document is written clearly: the message content of the reply is not more than 2048 bytes long. So why is it that a lot of people do not respond when they test the response message content at more than 1300 bytes in length? I think this question should be in this part of the people are not figuring out exactly how to calculate the number of bytes of text.

How to correctly calculate the number of bytes in a text

Calculate the number of bytes of text (string), the first thought should be the String class GetBytes () method, the method returns a string corresponding to the byte array, and then calculate the length of the array to get the number of bytes of the string. For example:

public static void Main (String []args)  {//Run Result: 4system.out.println ("Liu Feng". GetBytes (). length);}
In the example above, the two bytes in Chinese are calculated as 4, that is, a Chinese character occupies 2 bytes. Is that really the case? In fact, we have overlooked a problem: for different encoding methods, the Chinese accounted for the same number of bytes! What the hell is going on here? In the above example, we do not specify the encoding method, then the operating system will use the default encoding. Let me first look at the three conclusions I came up with:

1) If the above example runs on an operating system platform with the default encoding of iso8859-1, the result is 2;

2) If the above example runs on an operating system platform with the default encoding of gb2312 or GBK, the result is 4;

3) If the above example runs on an operating system platform with the default encoding of Utf-8, the result is 6;

If so, does that mean that the String.getbytes () method defaults to gb2312 or GBK encoding on our system platform? Let's look at one more example:

public static void Main (String []args) throws Unsupportedencodingexception  {//Run Result: 2system.out.println ("Liu Feng". GetBytes ("Iso8859-1"). length);//Run Result: 4system.out.println ("Liu Feng". GetBytes ("GB2312"). length);// Running Result: 4system.out.println ("Liu Feng". GetBytes ("GBK"). length);//Run Result: 6system.out.println ("Liu Feng". GetBytes ("UTF-8"). length );}
Is this example a good proof of the three conclusions I gave above? In other words, when using iso8859-1 encoding, a Chinese/English is only one byte; when using GB2312 or GBK encoding, an English is two bytes, while in UTF-8 encoding, a Chinese account is three bytes.

The encoding method used in the platform and the calculation of the byte count of the string

So, what encoding does it take to return a message to the server? Of course it is UTF-8, because we have used the following code in the Dopost method to avoid Chinese garbled:

The encoding of the request and response is set to UTF-8 (prevent Chinese garbled) request.setcharacterencoding ("UTF-8"); Response.setcharacterencoding ("UTF-8");
To verify what I have said, I have written an example to test:

private static String getmsgcontent () {StringBuffer buffer = new StringBuffer ();//70 characters per line, A total of 682 Chinese characters plus 1 English exclamation mark Buffer.append ("Do not know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you"); Buffer.append ("Do not know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you"); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you see you I lost my own good want to hold your hand through the ups and downs have any difficulties I accompany you "); Buffer.append (" Don't know when to start to like here every night will come here to see you how beautiful you look I can not see you do not see you I lost my own good want to hold! "); return buffer.tostring ();} PublIC static void Main (String []args) throws Exception {//gb2312 encoded as 1365 bytes System.out.println (getmsgcontent (). GetBytes ("gb2312"). Length),//2047 bytes System.out.println (getmsgcontent () when using Utf-8 encoding. GetBytes ("Utf-8"). length);}

The content returned by the Getmsgcontent () method is the longest text message that can be supported, that is, when the UTF-8 encoding is used, the text message content supports up to 2047 bytes, which means that the message content of the reply in the public platform interface document is not more than 2048 bytes in length, Even if it is equal to 2048 bytes, you can try to add an English symbol to the contents of the Getmsgcontent () method, which will not respond at this time.

At the same time, we also found that if gb2312 encoding is used to calculate the number of bytes returned by the Getmsgcontent () method, the result is 1365, which is why many friends say that the maximum length of a text message seems to support only 1300 multibyte, This is not the 2048 bytes in the interface document, but it ignores the encoding, but simply uses the GetBytes () method of the string class instead of the GetBytes ("Utf-8") method to calculate the number of bytes.

The method of calculating the number of bytes Utf-8 encoding in Java package

/** * Calculates the number of bytes in a string when using UTF-8 encoding *  * @param content * @return */public static int getbytesize (string content) {int size = 0;IF (Null! = content) {try {//Kanji takes utf-8 encoding for 3 bytes size = content.getbytes ("Utf-8"). Length;} catch ( Unsupportedencodingexception e) {e.printstacktrace ();}} return size;}

Well, here's what this chapter is about, and I think what you've learned through this article is that it's not just the number 2047, but also a new understanding of how character encoding works.


http://www.bkjia.com/PHPjc/444571.html www.bkjia.com true http://www.bkjia.com/PHPjc/444571.html techarticle I believe many friends have encountered this problem: when the text message sent is too long, will not do any response. So what is the maximum length of text messages that are allowed ...?

  • 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.