สร้างส่วนหัว HTTP
url สตริงสุดท้ายคงที่ส่วนตัว = "url"; สตริงสุดท้ายคงที่ส่วนตัว app_key = "key"; สตริงสุดท้ายคงที่ secret_key = "secret";
/ ** * สร้างข้อมูลส่วนหัวการรับรองความถูกต้องของการรับรองความถูกต้องพื้นฐาน * * @return */ สตริงส่วนตัว getheader () {string auth = app_key + ":" + secret_key; ไบต์ [] encodedauth = base64.encodeBase64 (auth.getBytes (charset.forName ("us-ascii")))); สตริง authheader = "พื้นฐาน" + สตริงใหม่ (encodedauth); ส่งคืน Authheader; -Old Way:
โมฆะส่วนตัว send1 (jpushobject pushObject) {closeablehttpClient client = httpClients.createdefault (); httppost post = new httppost (url); System.out.println ("ข้อมูลที่จะส่ง" + json.tojsonstring (pushobject)); Stresentity MyEntity = new StressEntity (json.tojsonstring (pushObject), contentType.application_json); // สร้างการร้องขอข้อมูลโพสต์. addheader ("การอนุญาต", getheader ()); post.setEntity (myEntity); // ตั้งค่าการตอบสนองของสตริงการร้องขอการตอบสนอง = null; // การตอบสนองเนื้อหาที่ปิดการตอบสนองการตอบสนอง = null; ลอง {response = client.execute (โพสต์); System.out.println (json.tojsonstring (การตอบสนอง)); if (response.getStatusline (). getStatusCode () == 200) {httpentity entity = response.getEntity (); Responsecontent = entityutils.toString (เอนทิตี, "UTF-8"); } System.out.println ("Responsecontent:" + Responsecontent); } catch (clientprotocolexception e) {e.printstacktrace (); } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (ตอบกลับ! = null) response.close (); } catch (ioexception e) {e.printstacktrace (); } ในที่สุด {ลอง {ถ้า (ไคลเอนต์! = null) client.close (); } catch (ioexception e) {e.printstacktrace (); -วิธี httpClient
โมฆะสาธารณะส่ง () พ่น clientprotocolexception, ioexception {httpClient httpClient = httpClientBuilder.create (). build (); httppost httppost = basehttppost.buildhttpheader (url); // ตั้งค่ารายการพารามิเตอร์ที่ร้องขอ <NameValuePair> nvps = arrayList ใหม่ <NameValuePair> (); NVPS.ADD (ใหม่ BasicNameValuePair ("FromAccid", จาก CACCID)); NVPS.ADD (ใหม่ BasicNameValuePair ("ToactaChys", toaccids)); NVPS.ADD (ใหม่ BasicAnmeValuePair ("type", msgType)); แผนที่ <สตริงวัตถุ> body = new hashmap <string, object> (); body.put ("msg", msg); nvps.add (ใหม่ BasicNameValuePair ("Body", Json.tojsonstring (ร่างกาย))); NVPS.ADD (ใหม่ BasicNameValuePair ("pushcontent", msg)); httppost.setEntity (urlencodedFormentity ใหม่ (NVPS, "UTF-8")); // ดำเนินการตามคำขอ httpresponse = httpClient.execute (httppost); // พิมพ์ผลการดำเนินการ System.out.println (entityutils.toString (response.getEntity (), "UTF-8")); -Java ด้านบนส่งรหัสตัวอย่างสำหรับคำขอโพสต์ HTTP พร้อมการตรวจสอบความถูกต้องพื้นฐานคือเนื้อหาทั้งหมดที่ฉันแบ่งปันกับคุณ ฉันหวังว่ามันจะให้ข้อมูลอ้างอิงและฉันหวังว่าคุณจะสนับสนุน wulin.com มากขึ้น