HTTP 헤더를 구성하십시오
개인 정적 최종 문자열 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; 바이트 [] encodedauth = base64.encodebase64 (auth.getBytes (charset.forname ( "us-Ascii"))); 문자열 authheader = "Basic" + new String (encodedauth); Return Authheader; }오래된 방법 :
private void send1 (jpushObject pushObject) {closeablehttpclient client = httpclients.createdefault (); httppost post = new httppost (URL); System.out.println ( "전송 될 데이터" + json.tojsonstring (pushobject)); 엄격한 myentity = new stramentity (json.tojsonstring (pushobject), contenttype.application_json); // 요청 요청 데이터 post.addheader ( "Authorization", getheader ()); post.setentity (myentity); // 요청 본문 문자열 resongecontent = null; // 응답 컨텐츠 ClosableHttTpresponse 응답 = null; try {response = client.execute (post); 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 (); } 마침내 {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); // 요청 된 매개 변수 목록을 설정 <NamesValuePair> nvps = new ArrayList <NamesValuePair> (); 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. // 요청 요청 httpresponse response = httpclient.execute (httppost); // 인쇄 실행 결과 system.out.println (entityutils.toString (response.getEntity (), "utf-8")); }위의 Java는 기본 인증 인증을 통해 HTTP Post 요청에 대한 예제 코드를 보냅니다. 나는 그것이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.