我现在调用一个https的API,用java写在后端去调用接口,但是总是报错,而且也不知道代码写的对不对,
给的token例如是这样的   aaa SUNKJNKGTDTCHBJINKMPOMLPML
设置token用不用加上那个aaa 后边有空格嘛
package com.warefont;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;public class test { public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
// System.out.println(test());
String url="https://cm......nship/5abe33a1fe2ee10006a06fa?aPiReq=true";
getUserData(url);
} public static void  getUserData(String url) throws ClientProtocolException, IOException {
// 1.创建 HttpClient 的实例 BasicHttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
DefaultHttpClient client = new DefaultHttpClient(httpParams);
HttpPost post = new HttpPost(url);
post.setHeader("Content-Type", "application/json");

String tokenStr ="eyJhbGciOiJIUzUxMiJ9.eyJKU1dfVE9LRU5fS0VZF0aW......NNXIfE6fn0Ps7d2nX8V1UU083_LwOTh4V6AVsQ";
// Basic YFUDIBGDJHFK78HFJDHF== token的格式
String token = "Monitoring " + tokenStr;
// 把认证信息发到header中
post.setHeader("Authorization",token);
// StringEntity stringEntity = new StringEntity("");//param参数,可以为"key1=value1&key2=value2"的一串字符串
// post.setEntity(stringEntity);
HttpResponse response = client.execute(post);
int statusCode = response.getStatusLine().getStatusCode();
String retSrc = EntityUtils.toString(response.getEntity(), "utf-8");
System.out.println("url============="+url);
System.out.println("statuscode============="+statusCode);
System.out.println("retSrc============="+retSrc);
//Log.i("Tag", "url==" + url);
//Log.i("Tag", "===statuscode===" + statusCode + "===retsrc===" + retSrc);

}
}

解决方案 »

  1.   

    HttpPost post = new HttpPost(url); 你这里用的post请求,
    但是你的String url="https://cm......nship/5abe33a1fe2ee10006a06fa?aPiReq=true"; 是get请求格式呀
      

  2.   

    用postman使用相同参数访问试试,看看是不是同一个错误,如果不是,再把url直接放到地址栏里请求,看提示什么,正常来讲如果限制get和post方式的话,应该是405状态码错误。
      

  3.   

    补充一下:postman使用post方式请求
      

  4.   

    使用HttpClient调用https的API调用需要指定方式为https
      

  5.   

    好的 谢谢 我今天用postman测试测试
      

  6.   

    这个要怎么指定呢
    还有就是我想问一个问题,https需要安全认证,那后台去调用,需要绕过安全认证嘛
      

  7.   

    这个要怎么指定呢
    还有就是我想问一个问题,https需要安全认证,那后台去调用,需要绕过安全认证嘛创建HttpHost时指定,httpHost = new HttpHost(_url.getHost(),_url.getPort(),“https”);
    需要绕过安全认证,或有证书可以导入证书。
    绕过验证部分代码:try {
    sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (KeyManagementException e) {
    e.printStackTrace();
    }
    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    sslcontext = SSLContexts.createSystemDefault();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
    .register("http", PlainConnectionSocketFactory.INSTANCE)
    // .register("https", new SSLConnectionSocketFactory(sslcontext)).build();
    .register("https", sslsf).build();
      

  8.   

    这个要怎么指定呢
    还有就是我想问一个问题,https需要安全认证,那后台去调用,需要绕过安全认证嘛创建HttpHost时指定,httpHost = new HttpHost(_url.getHost(),_url.getPort(),“https”);
    需要绕过安全认证,或有证书可以导入证书。
    绕过验证部分代码:try {
    sslsf = new SSLConnectionSocketFactory(builder.build(), new String[]{"SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.2"}, null, NoopHostnameVerifier.INSTANCE);
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    } catch (KeyManagementException e) {
    e.printStackTrace();
    }
    // SSL context for secure connections can be created either based on
    // system or application specific properties.
    sslcontext = SSLContexts.createSystemDefault();

    // Create a registry of custom connection socket factories for supported
    // protocol schemes.
    socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
    .register("http", PlainConnectionSocketFactory.INSTANCE)
    // .register("https", new SSLConnectionSocketFactory(sslcontext)).build();
    .register("https", sslsf).build();
    好的 谢谢~