Bangun header http
Private Static Final String URL = "URL"; Private Static Final String App_key = "Key"; Private Static Final String Secret_Key = "Secret";
/ ** * Bangun informasi header otentikasi Auth Dasar * * @return */ private string getHeader () {string auth = app_key + ":" + Secret_key; byte [] encodedauth = base64.encodeBase64 (auth.getbytes (charset.forname ("us-ascii")))); String authheader = "dasar" + string baru (encodedauth); Return Authheader; }Cara lama:
private void send1 (jPushObject PushObject) {closeableHttpClient client = httpclients.createDefault (); Httppost post = httpppost baru (url); System.out.println ("Data yang akan dikirim" + json.toJsonstring (PushObject)); StringEntity myEntity = new stringEntity (json.toJsonstring (pushObject), contentType.application_json); // Bangun Posting Data Permintaan. POST.SetEntity (MyEntity); // Atur responseContent string body permintaan = null; // Konten Respons Tutup HttPResponse Respons = NULL; coba {response = client.execute (post); System.out.println (json.tojsonstring (respons)); if (response.getstatusLine (). getStatusCode () == 200) {httpentity entity = response.getEntity (); responseContent = entityutils.tostring (entitas, "UTF-8"); } System.out.println ("ResponseContent:" + ResponseContent); } catch (clientprotocolexception e) {e.printstacktrace (); } catch (ioException e) {e.printstacktrace (); } akhirnya {coba {if (response! = null) response.close (); } catch (ioException e) {e.printstacktrace (); } akhirnya {coba {if (klien! = null) client.close (); } catch (ioException e) {e.printstacktrace (); }}}}Metode httpclient
public void send () melempar ClientProtocolException, IoException {httpClient httpClient = httpClientBuilder.create (). build (); Httppost httppost = BASEHTTPPOST.BUILDHTTPHEADER (URL); // Atur daftar parameter yang diminta <namevaluePair> nvps = ArrayList baru <namevaluePair> (); nvps.add (basicnamevaluePair baru ("fromAccid", fromAccid)); nvps.add (basicnamevaluePair baru ("toaccids", toaccids)); nvps.add (basicnamevaluePair baru ("type", msgtype)); Peta <string, object> body = hashmap baru <string, object> (); body.put ("msg", msg); nvps.add (basicnamevaluePair baru ("tubuh", json.toJsonstring (tubuh)))); nvps.add (basicnamevaluePair baru ("PushContent", MSG)); httppost.setentity (UrlencodedFormentity baru (NVP, "UTF-8"))); // Jalankan permintaan httpresponse response = httpclient.execute (httppost); // Cetak Eksekusi Hasil System.out.println (entityutils.tostring (response.getEntity (), "UTF-8")); }Java di atas mengirimkan kode contoh untuk permintaan posting http dengan otentikasi auth dasar adalah semua konten yang saya bagikan dengan Anda. Saya harap ini dapat memberi Anda referensi dan saya harap Anda dapat mendukung wulin.com lebih lanjut.