Construire en en-tête HTTP
Url de chaîne finale statique privée = "url"; chaîne finale statique privée app_key = "key"; chaîne finale statique privée secret_key = "secret";
/ ** * Construire des informations d'en-tête d'authentification de base Auth * * @return * / private String getheader () {String auth = app_key + ":" + secret_key; BYTE [] EncodedAuth = Base64.EncodeBase64 (Auth.GetBytes (charSet.Forname ("US-ASCII"))); String authheader = "Basic" + Nouvelle chaîne (encodauth); return authheader; }Voie ancienne:
private void Send1 (jpushObject pushObject) {CloseableHttpClient client = httpClients.CreateDefault (); HTTPPOST POST = NOUVEAU HTTPPOST (URL); System.out.println ("Données à envoyer" + JSON.TojSontring (PushObject)); StronEntity myentity = new stritetity (json.tojSontring (pushObject), contentType.Application_json); // Construire les données de la demande post.addheader ("Autorisation", getheader ()); post.SetEntity (MyEntity); // Définissez la demande de cordes de requête ResponseContent = null; // Response Content CloseableHttpResponse Response = null; essayez {réponse = client.execute (post); System.out.println (JSON.TojSontring (réponse)); if (réponse.getStatusline (). getStaturScode () == 200) {httpentity Entity = Response.getEntity (); ResponseContent = EntityUtils.ToString (Entity, "UTF-8"); } System.out.println ("ResponseContent:" + ResponseContent); } catch (clientProtoCoLexception e) {e.printStackTrace (); } catch (ioException e) {e.printStackTrace (); } enfin {try {if (réponse! = null) réponse.close (); } catch (ioException e) {e.printStackTrace (); } enfin {try {if (client! = null) client.close (); } catch (ioException e) {e.printStackTrace (); }}}}Méthode httpclient
public void Send () lève ClientProtoCoLexception, ioException {httpClient httpClient = httpClientBuilder.create (). build (); Httppost httppost = basehttppost.buildHttPheader (URL); // Définissez la liste des paramètres demandés <NameValuePair> nvps = new ArrayList <NameValuePair> (); nvps.add (new BasicNameValuepair ("FromAccid", FromAccid)); nvps.add (new BasicNameValuepair ("toACCIDS", toAcCids)); nvps.add (nouveau BasicNameValuePair ("type", msgtype)); Map <string, object> body = new HashMap <String, object> (); body.put ("msg", msg); nvps.add (new BasicNameValuepair ("Body", JSON.tojSontring (corps))); nvps.add (new BasicNameValuePair ("PushContent", msg)); httppost.SetEntity (new UrLencomedFormentity (NVPS, "UTF-8")); // Exécuter la demande httpResponse Response = httpClient.Execute (httppost); // imprimez l'exécution de résultats System.out.println (entityUtils.ToString (réponse.getEntity (), "UTF-8")); }Le Java ci-dessus envoie un exemple de code pour la demande de post HTTP avec l'authentification de base Auth est tout le contenu que je partage avec vous. J'espère que cela pourra vous donner une référence et j'espère que vous pourrez soutenir Wulin.com plus.