Many people think that QQ's HTTP server is based on 80 port communication (such as: 218.17.209.23: 80), in fact not like this, the Real Server Based on HTTP should be: http://tqq.tencent.com: 8000, it is a server that communicates through 8000 ports. The HTTP server of QQ does not support the get method in the HTTP protocol. It supports the POST method. Therefore, if we want to transmit parameters to the HTTP protocol of QQ, we must use the POST method.
Next we will introduce how to implement http post communication.
<1> C #:
In C #, The system. web space provides an object called WebClient. With this object, C # can directly send Web client requests to the server.
To submit the POST method to the server, you must use its uploaddata () method. First, convert the request information to byte (because post submits the stream data of characters) and use it as the uploaddata () parameter. Use uploaddata () for data submission, and finally return the post feedback. As follows:
WebClient _ client = new WebClient ();
String postvalues = "ver = 1.0 & cmd = query_stat & seq = 12321 & uin = 29501213 & Tn = 50 & UN = 0 ";
Byte [] bytearray = system. Text. encoding. ASCII. getbytes (postvalues );
Byte [] pagedata = _ client. uploaddata (host, "Post", bytearray );
In this way, we use C # To submit an http post method.
<2> Delphi:
In Delphi, we use a popular third-party VCL and Indy HTTP (which comes with this component D6 and D7) for HTTP Communication. The post method can be used for http post communication. When the value is passed in and returned, it is based on the UTF-8, C # shows Chinese is very often, but Delphi will be converted to UTF-8.
Achieve QQ User Login
In QQ communication, users must log on before sending information to each other. Qq login is very important. The user we see is online, not the user's QQ has been connected to the server, but regularly sent a message to the server, proving that they are still connected to the line, if it exceeds the time limit, QQ considers the user to be offline.
In the logon protocol, the QQ password is encrypted with the standard MD5. Delphi users only need the next MD5 encryption module, while C # has it, but it cannot be used directly. It must be processed before it can be converted into a standard MD5,
After learning about how QQ encrypts user passwords, we began to parse QQ's HTTP logon protocol. We passed the Protocol as a post parameter to the server, the server returns the corresponding information to the client:
Incoming protocol:
Ver = 1.1 & cmd = login & seq = & uin = & PS = & M5 = 1 & lc = 9326b87b234e7235
Ver is used to describe the version of the QQ protocol, CMD is the command to describe the protocol, login is the login of QQ, seq is a mark set by him to prevent repeated sending, generally, we can place a section of the current time value. (C #: datetime. now. ticks. tostring (). substring () Delphi: copystr (inttostr (gettickcount (),). uin indicates the QQ number, PS, is the value of the MD5 encrypted password.
Response Protocol:
Ver = 1.1 & cmd = login & seq = 11281 & uin = & res = 0 & rs = 0 & HI = 60 & Li = 300 (successful)
If RES is 0, a success is returned. If RS is 0, the logon is successful.
Ver = 1.1 & cmd = login & seq = 11422 & uin = 315103947 & res = 0 & rs = 1 & RA = Logon Failed
If RS is set to 1, the logon fails. Then, a prompt is displayed, indicating the cause.
Obtain QQ list:
If you add your friends, your friends will be put into your QQ friends list, to obtain the QQ list, we must send an agreement to the QQ server to obtain the friends list.
Ver = 1.1 & cmd = List & seq = & uin = & Tn = 160 & UN = 0
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = List & seq = 43661 & uin = 29501213 & res = 0 & FN = 1 & Sn = 24 & UN = 561256,1943497 ,....
Your friend's QQ number is next to UN, and each number is separated. Then we only need to get the code after UN and list it as OK. C # You can use string. Split (',') to put values into the list for processing, while Delphi can use split () to put values into tstrings for processing.
Obtain QQ friends online list
Obtaining the QQ friends online list is similar to obtaining the friends list. The only difference is that query_stat is used for different commands. The Protocol is as follows:
Ver = 1.1 & cmd = query_stat & seq = & uin = & Tn = 50 & UN = 0
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = query_stat & seq =-1 & uin = 29501213 & res = 0 & fc = 141,270,270, & FN = 1 & Sn = 3 & St = 10, 10, 10, & UN = 12327207,24259132, 29501213, & nk = □, Weicheng, Zookeeper ,&
FCIS the ID of the QQ image. For example, the ID of the image is, and the first image is 91.bmp. The algorithm is ID/3 + 1. St is the status of QQ users, 10 is online, 20 is offline, and 30 is busy. UN is the QQ number of online users, and NK is the QQ nickname of online users. St, UN, NK, and data separated by commas correspond to each other. If you use the Delphi language after obtaining the message, use utf8toansi () for conversion. Otherwise, garbled characters may occur.
Obtain QQ user information
If you want to see the QQ user's real name, mail, age, personal instructions, and other information, you must send a friend information to the server:
Ver = 1.1 & cmd = getinfo & seq = & uin = & Lv = 2 & UN =
UN is the QQ number for viewing user information.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = getinfo & seq = 12707 & uin = 415103947 & res = 0 & ad = Kunming, Yunnan & Ag = 0 & EM = Microprogramer@hotmail.com & fc = 270 & HP = msger.org (under construction ...) & JB = programmer & Lv = 2 & PC = 650000 & pH = 0871-6466529 & pr = the network is the media % 252c relationship. This % 252c information is mainly for customers of finance % 252c. % 0d % 0a & Pv = Yunnan Province & Rn = Liu X & SC = & SX = 0 & UN = 24259132 & nk = micro-cheng Ru
The contact address of an ad user. AG indicates the user's age, EM indicates the user's mail, FC indicates the user's profile picture, HP indicates the user's website, JB indicates the user's occupation, and PC indicates the user's zip code, pH indicates the user's contact number, PR indicates the user's profile, PV indicates the user's province, RN indicates the user's real name, SC indicates the user's graduation institution, SX indicates the user's gender, and UN indicates the user's QQ number, NK is the nickname of the user QQ. If you use the Delphi language after obtaining the message, use utf8toansi () for conversion. Otherwise, garbled characters will appear.
Add QQ friends
To add a friend, you need to send the addtolist command to the server. The specific command is as follows:
Ver = 1.1 & cmd = addtolist & seq = & uin = & UN =
UN adds the user's QQ number for us.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = addtolist & seq = 13666 & uin = 415103947 & res = 0 & Cd = 0 & UN = 24259132
CD indicates the identity authentication status of the added QQ account. If CD is set to 0, it indicates "allow anyone to list me as a friend", and if CD is set to 1, it indicates "you need to recognize your ID card to list me as a friend ", if CD is 3, "no one is allowed to list me as a friend ". If the value of CD is 0, after the information is returned, the user directly adds it as a friend. If the value of CD is 1, a response should be sent and added as a friend.
Response to a friend
The response is added as a friend by both parties: 1. If you send a request and add a friend as a friend, if the other party needs verification, you must send a response to add a friend as a friend. 2. If the other party sends a friend request to you, you can add a friend response. One is to add a friend, and the other is to pass verification, the first is to reject a friend. We want to send a command to the server:
Ver = 1.1 & cmd = ack_addtolist & seq = & uin = & UN = & Cd = & rs =
CD is the response status, and CD is 0, which indicates "Verification passed ". If the value of CD is 1, it indicates that "the peer is rejected as a friend ". If CD is set to 2, "add a friend to the requester" is displayed ". RS is the reason for your request. If you use Delphi, RS must be converted to ansitoutf8 () during sending. Otherwise, the reason for the request will change to "?" After sending.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = ack_addtolist & seq = 1130 & uin = 415103947 & res = 0 &
Delete friends
It is easy to delete friends. If you send the delfromlist command to the server, you can delete users:
Ver = 1.1 & cmd = delfromlist & seq = & uin = & UN =
UN indicates the QQ number of the user to be deleted.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = delfromlist & seq = 24514 & uin = 415103947 & res = 0 &
Change the current user status
You can set QQ to the online or invisible status. We can send change_stat to the server to change the current status. The specific command is as follows:
Ver = 1.1 & cmd = change_stat & seq = & uin = & St =
St is the status to be changed, 10 is online, 20 is offline, and 30 is busy.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = change_stat & seq = 17512 & uin = 415103947 & res = 0 &
Log out
To log out, send the logout command to the server. The specific command is as follows:
Ver = 1.1 & cmd = logout & seq = & uin =
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = logout & seq = 15803 & uin = 415103947 & res = 0
Obtain QQ messages from friends
To receive messages from friends, send the getmsgex command to the server. The specific command is as follows:
Ver = 1.1 & cmd = getmsgex & seq = & uin =
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = getmsgex & seq = 56661 & uin = 29501213 & res = 0 & Mn = 3 & mt = 99,9, 9, & UN = 24259132,24259132, 24259132, & Mg = 30, asdfasdfasdfasdf, asdfasdfasdf ,&
Mt indicates the message type, 99 indicates the system message, and 9 indicates the user message. "UN" indicates the user who sent the message, "mg" indicates the message sent, and "mg" indicates some specific system meanings. For example, when MT is 99 and Mg is 30, if the value of UN is 24259132, the user 4259132 is in a busy state. You can refresh the friends list based on the message to Improve the efficiency. If you use the Delphi language after obtaining the message, use utf8toansi () for conversion. Otherwise, garbled characters may occur.
Send messages to friends QQ
To send a message to a friend, send the cltmsg command to the server. The specific command is as follows:
Ver = 1.1 & cmd = cltmsg & seq = & uin = & UN = & Mg =
UN indicates the QQ number of the user who sent the message, and Mg indicates the message sent to the user. If you use Delphi, the MG must be converted to ansitoutf8 () during sending. Otherwise, the message will change to "?" After sending.
If the server receives the Protocol successfully, return:
Ver = 1.1 & cmd = cltmsg & seq = 15803 & uin = 415103947 & res = 0