Konstruktion HTTP -Header
private statische endgültige Zeichenfolge url = "url"; private statische endgültige String app_key = "key"; private statische endgültige String Secret_key = "Secret";
/ ** * Construct Basic Authentication Header Information * * @return */ private String getheader () {String auth = app_key + ":" + secry_key; byte [] codedAuth = base64.EncodeBase64 (auth.getBytes (charset.forname ("us-ascii"))); String authHeader = "Basic" + New String (codedAuth); Return AuthHeader; }Alter Weg:
private void send1 (jpushObject pushObject) {ClodableHttpclient Client = httpclients.CreateFault (); Httppost post = new httppost (URL); System.out.println ("Daten zu senden" + json.tojonstring (pushObject)); StrackEntity myentity = new strietentity (json.tojonstring (pushObject), contentType.application_json); // Konstruktieren Sie den Anforderungsdatenpost.AddHeader ("Autorisierung", getHeader ()); post.setentity (myEntity); // Setzen Sie die Anforderung des Anforderungskörperstring -ResponseContent = null; // Antwortinhalte CloseableHttPesponse Antwort = NULL; try {response = client.execute (post); System.out.println (json.tojonstring (Antwort)); if (response.getStatusLine (). getStatusCode () == 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 (); } endlich {try {if (response! = null) response.close (); } catch (ioException e) {e.printstacktrace (); } endlich {try {if (client! = null) client.close (); } catch (ioException e) {e.printstacktrace (); }}}}Httpclient -Methode
public void send () löst ClientProtocolException, ioException {httpclient httpclient = httpclientBuilder.create (). Build (); Httppost httppost = baseHttppost.buildhttpHeader (URL); // Setzen Sie die angeforderte Parameterliste <nameValuePair> nvps = new ArrayList <NameValuePair> (); NVPS.Add (New BasicNamevaluePair ("Fromaccid", Fromaccid)); NVPS.Add (New BasicNamevaluePair ("tocids", tocids)); NVPS.Add (New BasicNamevaluePair ("Typ", msgType)); Karte <String, Object> Body = New HashMap <String, Object> (); body.put ("msg", msg); NVPS.Add (New BasicNamevaluePair ("Body", json.tojonstring (Körper))); NVPS.Add (New BasicNamevaluePair ("PushContent", MSG)); httppost.setEntity (neue urlencoded forderity (nvps, "utf-8")); // Anforderung ausführen httPresponse response = httpclient.execute (httppost); // Ausführungsergebnis system.out.println (entityutils.tostring (response.getEntity (), "utf-8")); }Das obige Java sendet einen Beispielcode für die HTTP -Postanforderung mit grundlegender Authentifizierung. Dies ist der gesamte Inhalt, den ich mit Ihnen teile. Ich hoffe, es kann Ihnen eine Referenz geben und ich hoffe, Sie können Wulin.com mehr unterstützen.