Java服务端获取360token时候报错:Server returned HTTP response code: 400 for URL

樊鹏飞 2014-07-07 09:15:15
报错内容:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: https://openapi.360.cn/oauth2/access_token
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.centurymedia.summer.test.TrustSSL.readContentFromPost(TrustSSL.java:77)
at com.centurymedia.summer.test.TestAndroidQihooAccessToken.main(TestAndroidQihooAccessToken.java:45)


源代码:
public class TestAndroidQihooAccessToken {
public static void main(String[] args) throws IOException {
//测试用例
String url = "https://openapi.360.cn/oauth2/access_token";
String parm = "";
TrustSSL.readContentFromPost(url, parm);
}
}

public static void readContentFromPost(String url,String param) throws IOException{
// Post请求的url,与get不同的是不需要带参数
//设置SSLContext
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },
new java.security.SecureRandom());
// 打开连接
URL console = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) console.openConnection();

connection.setSSLSocketFactory(sc.getSocketFactory());
// 设置是否向connection输出,因为这个是post请求,参数要放在
// http正文内,因此需要设为true
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
// URLConnection.setFollowRedirects是static函数,作用于所有的URLConnection对象。
// URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数
connection.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
// 意思是正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode
// 进行编码
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,
// 要注意的是connection.getOutputStream会隐含的进行connect。
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
//对含有空格的参数进行处理
// 正文,正文内容其实跟get的URL中'?'后的参数字符串一致
out.writeBytes(param);
out.flush();
out.close(); // flush and close
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"ISO88591-1"));//设置编码,否则中文乱码
String line="";
while ((line = reader.readLine()) != null){
System.out.println(line);
}
reader.close();
connection.disconnect();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}

求大神解。。。。
...全文
25144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
低调的可爱 2014-12-09
  • 打赏
  • 举报
回复
我也遇到了,怎么解决这个问题的
upupzealot 2014-09-14
  • 打赏
  • 举报
回复
这问题解决了么?
tony4geek 2014-07-07
  • 打赏
  • 举报
回复
你看看你个参数问题
樊鹏飞 2014-07-07
  • 打赏
  • 举报
回复
1楼大神说的对 我刚联系上360技术了 好像确实是传参问题 我正在核对
致知Fighting 2014-07-07
  • 打赏
  • 举报
回复
引用 2 楼 rui888 的回复:
参数应该是什么呢?
根据OAuth2的协议,这个url要传的参数应该是: post: code=${code}&grant_type=authorization_code&redirect_uri=${redirect_uri}&client_id=${client_id}&client_secret=${client_secret}
tony4geek 2014-07-07
  • 打赏
  • 举报
回复
致知Fighting 2014-07-07
  • 打赏
  • 举报
回复
400 请求出错 由于语法格式有误服务器无法理解此请求 这是http的定义,360的开放平台的文档里肯定有描述400是什么意思,八成是你传的参数有问题,你仔细看过360的文档之后再说

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧