HttpClient Impersonation Server Request

Source: Internet
Author: User

Creates a default HttpClient instance.
Closeablehttpclient httpclient = Httpclients.createdefault ();
Create HttpPost
HttpPost HttpPost =//new httppost ("http://crmetl.yishenghuo.com:8142/outserver/startUpByHand.htm");
New HttpPost ("http://localhost:8087/outserver/startUpByHand.htm");
To create a parameter queue
list<namevaluepair> formparams = new arraylist<namevaluepair> ();
Formparams.add (New Basicnamevaluepair ("UserName", "admin"));
Formparams.add (New Basicnamevaluepair ("PassWord", "123456"));
Urlencodedformentity uefentity;
try {
uefentity = new Urlencodedformentity ((list<? extends org.apache.http.namevaluepair>) formparams, "UTF-8");
Httppost.setentity (uefentity);
Closeablehttpresponse HttpResponse = Httpclient.execute (HttpPost);
try {
httpentity entity = httpresponse.getentity ();
if (Entity! = null) {
0: Update succeeded, 1: Update failed, 2: Update, 3: Less than 1 hours from last update
Jsonobject json = new Jsonobject ();
if ("0". Equals (Entity)) {
Json.put ("code", "0");
Json.put ("Message", "Update succeeded!") ");
}else if ("1". Equals (Entity)) {
Json.put ("Code", "1");
Json.put ("message", entity+ "update failed! ");
}else if ("2". Equals (Entity)) {
Json.put ("Code", "2");
Json.put ("message", entity+ "in the update!) ");
}else if ("3". Equals (Entity)) {
Json.put ("Code", "3");
Json.put ("message", entity+ "is less than 1 hours from the last update, please update it later.) ");
}
Json.put ("message", "Unknown error ... ");
Response.getwriter (). Print (json.tojsonstring ());
Logger.info ("Updated Results:" +entityutils.tostring (Entity, "UTF-8"));

}
} finally {
Httpresponse.close ();
}
} catch (Clientprotocolexception e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception E1) {
E1.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
Close the connection and release the resource
try {
Httpclient.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

HttpClient Impersonation Server Request

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.