HTTPヘッダーを構築します
private static final string url = "url"; private static final string app_key = "key"; private static final string secret_key = "secret";
/ ** *基本認証ヘッダー情報 * * @return */ 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 pushobject){closeablehttpclient client = httpclients.createdefault(); httppost post = new httppost(url); System.out.println( "送信されるデータ" + json.tojsonstring(pushObject)); Stringetity myEntity = new Stringentity(json.tojsonstring(pushobject)、contentType.application_json); post.setEntity(myentity); //リクエストボディ文字列responsecontent = nullを設定します。 //応答コンテンツCLOSEABLEHTTPRESPONSE Response = null; try {response = client.execute(post); system.out.println(json.tojsonstring(response)); if(respons.getStatusline()。getStatusCode()== 200){httpentity entity = respons.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)respons.close(); } catch(ioexception e){e.printstacktrace(); }最後に{try {if(client!= null)client.close(); } catch(ioexception e){e.printstacktrace(); }}}}httpclientメソッド
public void send()throws clientProtocolexception、ioexception {httpclient httpclient = httpclientbuilder.create()。build(); httppost httppost = basehttppost.buildhttpheader(url); //要求されたパラメーターリスト<NameValuePair> nvps = new ArrayList <NameValuePair>(); 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( "PushContent"、MSG)); httppost.setentity(new UrlencodedFormentity(nvps、 "utf-8")); //リクエストhttpresponse応答= httpclient.execute(httppost); //実行結果system.out.println(entityutils.toString(respons.getEntity()、 "utf-8")); }上記のJavaは、基本的な認証を使用してHTTP POSTリクエストのサンプルコードを送信します。私が共有するすべてのコンテンツです。私はそれがあなたに参照を与えることができることを願っています、そしてあなたがwulin.comをもっとサポートできることを願っています。