How to use http to Post blog comments

Source: Internet
Author: User
This article mainly introduces how to use the http protocol to publish blog post comments. First of all, you must make it clear that the essence of submitting comments to a blog post is to send a post request through the http server, for more information, see First, we will introduce the implementation principles:

The essence of submitting comments to a blog post is to send a post request through the http server. What do we need to do before publishing a comment? Yes, you must log on. However, login is another thing we will not discuss here. After a user logs on, the server sets a cookie for the client. Http is stateless. That is to say, after the client sends a request to the server, the server returns a response. One communication is complete. The server does not remember who sent the request to itself. Therefore, the client sends a request to the server with the cookie set by the server and notifies the server of its identity. the server generates a response based on the cookie.

Preparations:

In order to complete this test, I registered a blog post account (DeanHuangChopper). after logging on to the blog page, I opened my blog (DeanChopper) and opened one of my blog posts, for example, the article "understanding the buffer mechanism with php ob functions" (I am using Firefox, the biggest advantage is that the parameters sent to the server can be intuitively seen), open the developer option, prepare to record the process of sending comments. I write a comment and post a comment. This request has been recorded by the developer options.

We only need to pay attention to the request header.

Obviously, we can set the request header information through the setHeader () method of the Http class and send it through the post () method. First, we carefully analyze the request header information. Host, Content-type, Contetn-length will be set by default through some http methods, we can not add. Note that the Content-type is "application/json; charset = UTF-8", which is different from the default Http setting "application/x-www-form-urlencoded ". The request body is in json format, not an array format. Therefore, the original Http post method needs to be rewritten.

Public function post ($ body) {$ this-> setLine ('post'); // reset the content-type $ this-> setHeader ('content-Type: application/json; charset = UTF-8 '); // skip the setBody method // $ this-> setBody ($ body); $ this-> body [] = $ body; // calculate content-length $ this-> setHeader ('content-length :'. strlen ($ this-> body [0]); $ this-> request (); return $ this-> response ;}

After modifying the Http class again, we can write the main code in this article. Theoretically, you only need to set the cookie value when setting the header information, but it is best to send all the header information to improve the success rate.

Before sending a comment, take a look at the sent parameters:

You only need to enter the content you want to send after "body.

Code section:

The main code of this article is as follows:

<? Phprequire "http. class. php"; $ http = new Http (' http://www.cnblogs.com/mvc/PostComment/Add.aspx '); // Set the header information $ http-> setHeader ('Accept-Language: zh-CN, zh; q = 0.8, en-US; q = 0.5, en; q = 0.3 '); $ http-> setHeader ('Accept-Language: zh-CN, zh; q = 0.8, en-US; q = 0.5, en; q = 0.3 '); $ http-> setHeader ('Accept-Encoding: gzip, deflate'); $ http-> setHeader ('x-Requested-With: XMLHttpRequest '); $ http-> setHeader ('referer: http://www.cnblogs.com/DeanChopper/p/4688667.html '); $ Http-> setHeader ('cookie: _ ga = GA1.2.1359064105.1438444082; _ gads = ID = e0c32fd6db6e2a6d: T = 1438443900: S = Beijing ;. CNBlogsCookie = response; _ 5t_trace_sid = response; _ 5t_trace_tms = 1; _ gat = 1'); $ http-> setHeader ('pragma: no-cache '); $ http-> setHeader ('cache-Control: no-cache'); // Set the request body information $ msg = '{"blogApp": "DeanChopper", "postId ": 4688667, "body": "Test Content", "parentCommentId": 0} '; // send a post request $ http-> post ($ msg); echo' OK ';

The sending process may be slow. please wait.

The above is all the content of this article. I hope you will like 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.