بناء رأس HTTP
Static Static Final String url = "url" ؛ private static final string app_key = "key" ؛ private static final string secret_key = "secret" ؛
/ ** * إنشاء معلومات رأس المصادقة الأساسية * * * @REGING */ private string getheader () {String Auth = App_Key + ":" + secret_key ؛ byte [] encodedauth = base64.encodeBase64 (auth.getBytes (charset.forname ("us-ascii"))) ؛ String authHeader = "Basic" + New String (EncodEdauth) ؛ إرجاع Authheader. }الطريق القديم:
private void send1 (jpushObject propeBject) {closleablehttpclient client = httpclients.createdefault () ؛ httppost post = new httppost (url) ؛ System.out.println ("البيانات المراد إرسالها" + json.tojsonstring (propoBject)) ؛ myentity strityity = new itsentity (json.tojsonstring (protebject) ، contentType.application_json) ؛ // بناء طلب بيانات post.addheader ("إذن" ، getheader ()) ؛ post.setentity (myentity) ؛ // قم بتعيين طلب السلسلة الجسدية المسؤولية = null ؛ // استجابة محتوى القابلية للاستجابة للاستجابة = خالية ؛ حاول {response = client.execute (post) ؛ System.out.println (json.tojsonstring (response)) ؛ if (response.getStatusLine (). getStatusCode () == 200) {httpentity intity = response.getentity () ؛ AssisteContent = entityUtils.ToString (الكيان ، "UTF-8") ؛ } system.out.println ("Assistenent:" + AbseContent) ؛ } 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 () يلقي clientprotocolexception ، ioException {httpclient httpclient = httpclientbuilder.create (). build () ؛ httppost httppost = basehttppost.buildhttpheader (url) ؛ // قم بتعيين قائمة المعلمات المطلوبة <MameValUepair> nvps = new ArrayList <NameValUepair> () ؛ nvps.add (New BasicNameValuePair ("FromAccid" ، fromAccid)) ؛ nvps.add (New BasicNameValuePair ("toaccids" ، toAccids)) ؛ nvps.add (New BasicNameValuePair ("type" ، msgtype)) ؛ الخريطة <string ، object> body = new HashMap <string ، Object> () ؛ body.put ("msg" ، msg) ؛ nvps.add (New BasicNameValuePair ("body" ، json.tojsonstring (body))) ؛ NVPS.Add (New BasicNameValuePair ("PushContent" ، MSG)) ؛ httppost.setentity (urlencodedformentity (NVPs ، "UTF-8")) ؛ // تنفيذ طلب httpresponse استجابة = httpclient.execute (httppost) ؛ // print تنفيذ نتائج system.out.println (entityUtils.ToString (response.getentity () ، "utf-8")) ؛ }يرسل Java أعلاه رمز مثال لطلب نشر HTTP مع مصادقة المصادقة الأساسية هو كل المحتوى الذي أشاركه معك. آمل أن يعطيك مرجعًا وآمل أن تتمكن من دعم wulin.com أكثر.