Построить заголовок HTTP
частная статическая конечная строка url = "url"; частная статическая конечная строка app_key = "key"; частная статическая конечная строка secret_key = "secret";
/ ** * Создать базовую информацию об аутентификации аутентификации аутентификации * * @return */ private string getheader () {string auth = app_key + ":" + secret_key; byte [] ondedauth = base64.encodebase64 (auth.getbytes (charset.forname ("us-assii"))); String authheader = "basic" + new String (encodedauth); вернуть Authheader; }Старый путь:
private void send1 (jpushobject pushobject) {stockablehttpclient client = httpclients.createdefault (); Httppost post = new httppost (url); System.out.println («Данные для отправки» + json.tojsonstring (pushobject)); Strongity myEntity = new строгость (json.tojsonstring (pushobject), contentType.application_json); // Создание данных запроса. post.setentity (myEntity); // Установить строку для корпуса запроса responsecontent = null; // Содержание ответа QUEDBEALHTTPRESPONSE response = null; try {response = client.execute (post); System.out.println (json.tojsonstring (ответ)); 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 (); } наконец {try {if (response! = null) response.close (); } catch (ioException e) {e.printstackTrace (); } наконец {try {if (client! = null) client.close (); } catch (ioException e) {e.printstackTrace (); }}}}HTTPClient Метод
public void send () выпускает клиентский protocolexception, ioexception {httpclient httpclient = httpclientbuilder.create (). build (); Httppost httppost = basehttppost.buildhttphader (url); // Установить запрошенной список параметров <namevaluePair> nvps = new ArrayList <mankvaluePair> (); nvps.add (new basicnamevaluepair ("fromaccid", fromaccid)); nvps.add (new Basicnamevaluepair («Toaccids», Toaccids)); nvps.add (new basicnamevaluepair ("type", msgtype)); Map <string, object> body = new hashmap <string, object> (); body.put ("msg", msg); nvps.add (new basicnamevaluepair («body», json.tojsonstring (body))); nvps.add (new basicnamevaluepair («push -контейнер», MSG)); httppost.setentity (новый урлонедодизм (NVPS, "UTF-8")); // Выполнить запрос httpresponse response = httpclient.execute (httppost); // печатная система выполнения System.out.println (entityutils.tostring (response.getentity (), "utf-8")); }Приведенная выше Java отправляет пример кода для запроса POST HTTP с базовой аутентификацией Auth Authentication - это весь контент, которым я делюсь с вами. Я надеюсь, что это может дать вам ссылку, и я надеюсь, что вы сможете поддержать Wulin.com больше.