Android Analog Login Comment CSDN implementation code _android

Source: Internet
Author: User
Tags comments throwable

Sometimes, as an unofficial developer, the app integrates all the official information, but now it needs to implement another feature that is to log in and comment to the official website, while the unofficial app doesn't know how the official web site is going to log in and comment, and the larger the site, in order to prevent such things from happening, Added a lot of obstacles, but we can give you a common way, is a bit time-consuming, but in this way, basically all the site is nothing. Take CSDN to do the experiment today.

1. Login Csdn

View its source code look at its form form:

Its difficulty in the post data in the value of it and execution value, which is randomly generated, this estimate is csdn to prevent malicious login settings of the dynamic identity, but you can in Android to get the value and then set to the form to submit the list of items.

The following uses Xutils and Jsoup to get the page properties:

Requestparams params = new Requestparams ("Https://passport.csdn.net/account/login?ref=toolbar"); Params.setheader ("User-agent", "mozilla/5.0" (Windows NT 6.1; trident/7.0;
rv:11.0) like Gecko ");
    X.http (). Get (params, new callback.commoncallback<string> () {@Override public void onsuccess (String result) {
    LOG.I ("Csdn", "onsuccess");
    Requestparams params1=new requestparams ("Https://passport.csdn.net/account/login?ref=toolbar");
    Params1.setheader ("Accept", "text/html, Application/xhtml+xml, */*");
    Params1.setheader ("Content-type", "application/x-www-form-urlencoded");
    Params1.setheader ("DNT", "1");
    Params1.setheader ("Host", "passport.csdn.net");
    Params1.setheader ("Accept-language", "ZH-CN"); Params1.setheader ("User-agent", "mozilla/5.0" (Windows NT 6.1; trident/7.0;
    rv:11.0) like Gecko ");
    Document doc=jsoup.parse (Result);
    Element Formele=doc.getelementbyid ("FM1");
    Elements inputs=formele.getelementsbytag ("input"); for (Element INPUT:INPUTS) {if (input.attr ("name"). Equals ("LT")) {Params1.addquerystringparameter ("LT", Input.attr ("value")); }else if (input.attr ("name"). Equals ("Execution")) {Params1.addquerystringparameter ("execution", input.attr ("value
      ")); }else if (input.attr ("name"). Equals ("_eventid")) {Params1.addquerystringparameter ("_eventid", Input.attr ("value"))
      ;
    } params1.addquerystringparameter ("username", "your Account");
  Params1.addquerystringparameter ("Password", "Your password");
  @Override public void OnError (Throwable ex, Boolean isoncallback) {log.i ("csdn", "OnError");
  @Override public void oncancelled (Cancelledexception CeX) {log.i ("csdn", "oncancelled");
  @Override public void onfinished () {log.i ("csdn", "onfinished");

 }
});

This way we not only get the dynamic identity of the Web page, but also set it to the requestparams that will be submitting the form.

So we rebuilt a params1, this time all the form information has been set to it, the following simulation log in:

public void Getlogincsdn (final requestparams params) {
  x.http (). Request (Httpmethod.post, params, new Callback.commoncallback<string> () {
    @Override public
    void onsuccess (String result) {
      log.i (" Liyuanjinglyj "," onsuccess ");
      System.out.println (Result.tostring ());

    }

    @Override public
    void OnError (Throwable ex, Boolean isoncallback) {
      ex.printstacktrace ();
      LOG.I ("Liyuanjinglyj", "OnError");
    }

    @Override public
    void oncancelled (Cancelledexception CeX) {
      log.i ("Liyuanjinglyj", "oncancelled");
    }

    @Override public
    void onfinished () {
      log.i ("Liyuanjinglyj", "onfinished");
    }
  );

Calling GETLOGINCSDN will output the following information in the log:

2. Comment Csdn Blog

First of all we open a blog, the HttpWatch also open. Now enter the information in the comments, do not immediately click Submit, first open HttpWatch. Then submit and then close, and we'll get the following interface:

The form data submitted here is three, and there is a query String:
Why not open the HTML code here, let's look at the HTML code first:
Can you see anything? The only thing I see is the form in Div id=comment_form, but where does the submitted form item go? The answer is CSDN written in JS inside. Below we open its processing Comment_form JS.

People write here, you do not have a little time to really find. If you submit the form data according to the name of JS, you will not get any results. Returns 0 to end with failure.

Here we start setting up the relevant data for the comment into Requestparams:

 Requestparams requestparams = new Requestparams ("http://blog.csdn.net/ljy1988123/
Comment/submit ");
Requestparams.setconnecttimeout (15000); Requestparams.setheader ("User-agent", "mozilla/5.0" (Windows NT 6.1; trident/7.0;
rv:11.0) like Gecko ");
Requestparams.setheader ("Accept-language", "ZH-CN");
Requestparams.setheader ("Host", "blog.csdn.net");
Requestparams.setheader ("Referer", "http://blog.csdn.net/ljy1988123/article/details/50581207");
Requestparams.setheader ("Accept", "*/*");
Requestparams.setheader ("Content-type", "application/x-www-form-urlencoded");
Requestparams.setheader ("X-requested-with", "XMLHttpRequest");
Requestparams.setheader ("Connection", "keep-alive");
Requestparams.setmultipart (TRUE);
Requestparams.addquerystringparameter ("id", "50581207");
Requestparams.addparameter ("Commentid", ""); Requestparams.addparameter ("Content"), "like this blog too much."
98 ");
Requestparams.addparameter ("Replyid", "");
Sethttpcontent (Requestparams); 

Of course there is no need for the data in this, in order to test me in accordance with the settings given in HttpWatch:
Then invoke the POST request:

public void Sethttpcontent (Requestparams params) {
  x.http (). Post (params, new callback.commoncallback<string > () {
    @Override public
    void onsuccess (String result) {
      log.i ("Liyuanjinglyj", "onsuccess");
      System.out.println (Result.tostring ());
    }

    @Override public
    void OnError (Throwable ex, Boolean isoncallback) {
      ex.printstacktrace ();
      LOG.I ("Liyuanjinglyj", "OnError");
    }

    @Override public
    void oncancelled (Cancelledexception CeX) {
      log.i ("Liyuanjinglyj", "oncancelled");
    }

    @Override public
    void onfinished () {
      log.i ("Liyuanjinglyj", "onfinished");
    }
  );


The following log information will be output:

Its return 1 represents success, return 0 represents failure, the only need to explain is that I set the timeout is 15 seconds, why set such a long time, presumably everyone than I know, csdn in the web comments need so long time, not to mention the mobile phone end.

Look at the page effect:

One of the many is the test of the Super times wrong to publish up.

This article is just a mock login with comment csdn, and other sites are similar to this one.

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.