Currently, to provide an interface for another project, the interface is implemented with an HTTP URL, and the initial idea is that another project is requesting with the jquery post.
However, it is possible that another project is deployed on a different machine, so there is a cross-domain problem, and jquery's post request is not allowed across domains.
At this time, only enough httpclient package to request, and because the URL of the request is HTTPS, in order to avoid the need for certificates, so a class inherits the Defaulthttpclient class, ignoring the validation process.
1. Write a sslclient class, inherit to HttpClient
[Java]View PlainCopy
- Import java.security.cert.CertificateException;
- Import Java.security.cert.X509Certificate;
- Import Javax.net.ssl.SSLContext;
- Import Javax.net.ssl.TrustManager;
- Import Javax.net.ssl.X509TrustManager;
- Import Org.apache.http.conn.ClientConnectionManager;
- Import Org.apache.http.conn.scheme.Scheme;
- Import Org.apache.http.conn.scheme.SchemeRegistry;
- Import Org.apache.http.conn.ssl.SSLSocketFactory;
- Import org.apache.http.impl.client.DefaultHttpClient;
- HttpClient for HTTPS requests
- Public class Sslclient extends defaulthttpclient{
- Public sslclient () throws exception{
- super ();
- Sslcontext CTX = sslcontext.getinstance ("TLS");
- X509trustmanager TM = New X509trustmanager () {
- @Override
- public void checkclienttrusted (x509certificate[] chain,
- String authtype) throws certificateexception {
- }
- @Override
- public void checkservertrusted (x509certificate[] chain,
- String authtype) throws certificateexception {
- }
- @Override
- Public x509certificate[] Getacceptedissuers () {
- return null;
- }
- };
- Ctx.init (null, new TRUSTMANAGER[]{TM}, null);
- Sslsocketfactory SSF = new Sslsocketfactory (Ctx,sslsocketfactory.allow_all_hostname_verifier);
- Clientconnectionmanager CCM = This.getconnectionmanager ();
- schemeregistry sr = Ccm.getschemeregistry ();
- Sr.register (new Scheme ("https", 443, SSF));
- }
- }
2. Write a class that uses httpclient to send a POST request
[Java]View PlainCopy
- Import java.util.ArrayList;
- Import Java.util.Iterator;
- Import java.util.List;
- Import Java.util.Map;
- Import Java.util.Map.Entry;
- Import org.apache.http.HttpEntity;
- Import Org.apache.http.HttpResponse;
- Import Org.apache.http.NameValuePair;
- Import org.apache.http.client.HttpClient;
- Import org.apache.http.client.entity.UrlEncodedFormEntity;
- Import Org.apache.http.client.methods.HttpPost;
- Import Org.apache.http.message.BasicNameValuePair;
- Import Org.apache.http.util.EntityUtils;
- /*
- * Tool class for Post requests with httpclient
- */
- Public class Httpclientutil {
- Public string DoPost (String url,map<string,string> map,string charset) {
- HttpClient HttpClient = null;
- HttpPost httppost = null;
- String result = null;
- try{
- HttpClient = new Sslclient ();
- HttpPost = new HttpPost (URL);
- //Set parameters
- list<namevaluepair> list = new arraylist<namevaluepair> ();
- Iterator Iterator = Map.entryset (). Iterator ();
- While (Iterator.hasnext ()) {
- entry<string,string> Elem = (entry<string, string>) Iterator.next ();
- List.add (New Basicnamevaluepair (Elem.getkey (), Elem.getvalue ()));
- }
- if (list.size () > 0) {
- urlencodedformentity entity = new urlencodedformentity (List,charset);
- Httppost.setentity (entity);
- }
- HttpResponse response = Httpclient.execute (HttpPost);
- if (response! = null) {
- Httpentity resentity = response.getentity ();
- if (resentity! = null) {
- result = Entityutils.tostring (Resentity,charset);
- }
- }
- }catch (Exception ex) {
- Ex.printstacktrace ();
- }
- return result;
- }
- }
3. Call the test code for the POST request
[Java]View PlainCopy
- Import Java.util.HashMap;
- Import Java.util.Map;
- Testing the interface
- Public class Testmain {
- private String URL = "https://192.168.1.101/";
- private String charset = "Utf-8";
- private Httpclientutil httpclientutil = null;
- Public Testmain () {
- Httpclientutil = new Httpclientutil ();
- }
- public Void Test () {
- String httporgcreatetest = URL + "httporg/create";
- map<string,string> Createmap = new hashmap<string,string> ();
- Createmap.put ("Authuser","* * * * *");
- Createmap.put ("Authpass","* * * * *");
- Createmap.put ("Orgkey","* * *");
- Createmap.put ("OrgName","* * *");
- String Httporgcreatetestrtn = Httpclientutil.dopost (Httporgcreatetest,createmap,charset);
- System.out.println ("Result:" +httporgcreatetestrtn);
- }
- public static void Main (string[] args) {
- Testmain main = new Testmain ();
- Main.test ();
- }
- }
httpClient4.2 jar Package Download path: http://download.csdn.net/detail/hqmryang/4582440#comment
Java uses httpclient for post requests (HTTPS)