Construa o cabeçalho HTTP
String final estática privada URL = "URL"; String final estática privada app_key = "key"; private static final string secret_key = "secret";
/ ** * Construa informações básicas sobre autenticação de autenticação * * @return */ private string getheader () {string auth = app_key + ":" + secret_key; byte [] codedauth = base64.encodeBase64 (auth.getbytes (charset.formoname ("US-ascii"))); String authheader = "Basic" + New String (codedauth); retornar authheader; }Caminho antigo:
private void send1 (jpushObject pushObject) {closeablehttpclient client = httpclients.createFault (); Httppost post = novo httppost (url); System.out.println ("dados a serem enviados" + json.tojSonstring (pushObject)); Stringentity myEntity = new stringentity (json.tojSonstring (pushObject), contentType.application_json); // Construa a solicitação de dados post.addheader ("autorização", getheader ()); post.setEntity (myEntity); // Defina a solicitação da sequência de corpo RespoContent = null; // RESPOSTA CONTEÚDO EXCEUTILIDADEHTTPRESPOnsion Resposta = NULL; tente {Response = client.execute (post); System.out.println (json.tojSonstring (resposta)); if (Response.getStatusline (). getStatuscode () == 200) {entidade httPentity = Response.getEntity (); responseContent = entityutils.toString (entidade, "UTF-8"); } System.out.println ("ResponseContent:" + ResponseContent); } catch (clientprotocolexception e) {e.printStackTrace (); } catch (ioexception e) {e.printStackTrace (); } finalmente {tente {if (resposta! = null) resposta.close (); } catch (ioexception e) {e.printStackTrace (); } finalmente {tente {if (client! = null) client.close (); } catch (ioexception e) {e.printStackTrace (); }}}}Método HttpClient
public void send () lança clientprotocolexception, ioexception {httpclient httpClient = httpclientbuilder.create (). build (); Httppost httppost = basehttppost.buildhttpheader (url); // Defina a lista de parâmetros solicitados <NameValuepair> nvps = new ArrayList <NameValuepair> (); nvps.add (new BasicNameValuepair ("FromAccid", FromAccid)); nvps.add (new BasicNameValuepair ("TOACCIDS", TOACCIDS)); nvps.add (new BasicNameValuepair ("tipo", msgtype)); Mapa <string, object> body = new hashmap <string, object> (); body.put ("msg", msg); nvps.add (new BasicNameValuepair ("Body", json.tojSonstring (corpo))); nvps.add (new BasicNameValuepair ("PushContent", MSG)); httppost.Setentity (New UrlencodedFormentity (NVPS, "UTF-8")); // Execute a solicitação httproponse resposta = httpclient.execute (httppot); // Print Execution Result System.out.println (entityutils.toString (Response.getEntity (), "UTF-8")); }O Java acima envia um código de exemplo para solicitação de postagem HTTP com autenticação básica de autenticação é todo o conteúdo que compartilho com você. Espero que possa lhe dar uma referência e espero que você possa apoiar mais o wulin.com.