初識(shí)篇
1.0介紹
1.1自我介紹
大家好,我是IT擺爛工程師,今天正式在博客上發(fā)布文章,請(qǐng)大家多多支持!??!
1.2寫(xiě)csdn博客初衷
原因是記錄一些關(guān)于java的編程知識(shí),在工作中的一些困難點(diǎn),博主也是編程菜鳥(niǎo),希望在探索的過(guò)程中一步步變“禿”,最重要的是記錄自己的成長(zhǎng)歷程。
接下來(lái)就開(kāi)始本篇博客的主要內(nèi)容了
2.0對(duì)接接口
2.1java.io.IOException: Server returned HTTP response code: 400 for URL: http://xxxx
以上錯(cuò)誤是對(duì)接接口常見(jiàn)的錯(cuò)誤,是怎么形成的呢,菜鳥(niǎo)博主也是不太會(huì),所以記錄一下。
2.2解決
可能出現(xiàn)錯(cuò)誤的原因
- 可能是沒(méi)有做字符轉(zhuǎn)義
- 可能是沒(méi)有了解完全第三方接口
- ........
2.2.1第一種錯(cuò)誤,解決代碼如下
UrlUtil.sendGetHttpUrlUrlUtil.sendGetHttpUrlURLEncoder.encode("張三 =","UTF-8") //調(diào)用端
@GetMapping("/doGetLogin")
public Map<String, Object> doGetLogin() throws IOException {
System.out.println("調(diào)用方");
String urlstr="name=張三 =&pwd=123456="; //請(qǐng)注意張三后面有一個(gè)空格
String newUrl="http://127.0.0.1:7777/user/login"+"?"+urlstr;
String str = UrlUtil.sendGetHttpUrl(newUrl,"token"); //工具包網(wǎng)上有
Map map = JSONObject.parseObject(str, Map.class); //引入fastjson依賴
return map;
}
@GetMapping("/doGetLogin2")
public Map<String, Object> doGetLogin2() throws IOException {
System.out.println("調(diào)用方");
String urlstr ="name="+ URLEncoder.encode("張三 =","UTF-8")+"&pwd="+URLEncoder.encode("123456=","UTF-8");
String newUrl="http://127.0.0.1:7777/user/login"+"?"+urlstr;
String str = UrlUtil.sendGetHttpUrl(newUrl,"token");
Map map = JSONObject.parseObject(str, Map.class);
return map;
}
//第三方接口端
@GetMapping("/login")
public R login(@RequestParam(required = false) Map<String, Object> map, HttpServletRequest request) throws UnsupportedEncodingException {
System.out.println("模擬第三方接口");
System.out.println(map.toString());
System.out.println(request.getQueryString());
System.out.println(request.getHeader("Authorization")); //獲取token
if (map.get("name") != null && map.get("pwd") !=null) {
if (map.get("name").equals("張三 =") && map.get("pwd").equals("123456=")) {
return R.ok();
}
return R.error("賬號(hào)和密碼輸入不正確");
}else {
return R.error("賬號(hào)和密碼不能為空");
}
}
注意:
-
使用第一種方法會(huì)報(bào)錯(cuò),因?yàn)橛锌崭褡址?,沒(méi)有進(jìn)行轉(zhuǎn)義,所以會(huì)報(bào)錯(cuò)誤
java.io.IOException: Server returned HTTP response code: 400 for URL: http://127.0.0.1:7777/user/login?name=張三 =&pwd=123456= at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0] at java.lang.reflect.Constructor.newInstance(Constructor.java:408) ~[na:1.8.0] at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1888) ~[na:1.8.0] at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1883) ~[na:1.8.0] at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0] at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1882) ~[na:1.8.0] at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1455) ~[na:1.8.0] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) ~[na:1.8.0]
最主要的是name=張三 =&pwd=123456=,沒(méi)有進(jìn)行轉(zhuǎn)義
-
使用第二種方式進(jìn)行調(diào)用就不會(huì)出現(xiàn)錯(cuò)誤
name=%E5%BC%A0%E4%B8%89+%3D&pwd=123456%3D
原因是張三 =轉(zhuǎn)義成%E5%BC%A0%E4%B8%89+%3D這個(gè)字符串了,可以進(jìn)行http請(qǐng)求調(diào)用 空格轉(zhuǎn)為+號(hào)了,最主要的空格出現(xiàn)在url中會(huì)破壞這個(gè)http請(qǐng)求路徑,所有要進(jìn)行轉(zhuǎn)義。 -
URLEncoder.encode("張三 =","UTF-8") --java.net.URLEncoder
可以進(jìn)行對(duì)"張三 ="這個(gè)字符串轉(zhuǎn)義成 %E5%BC%A0%E4%B8%89+%3D -
服務(wù)器應(yīng)該是給url做了處理,所以不需要自己手動(dòng)進(jìn)行url路徑轉(zhuǎn)義回來(lái),但也可以手動(dòng)進(jìn)行轉(zhuǎn)義
System.out.println(URLDecoder.decode("轉(zhuǎn)義過(guò)后的字符串(%E5%BC%A0%E4%B8%89+%3D)", "UTF-8"));
可能有工具類(lèi)封裝了,但是博主沒(méi)找^_^
-
最后附上我的--UrlUtil.sendGetHttpUrl
/**
* get請(qǐng)求
* @param url 請(qǐng)求地址
* @param token 請(qǐng)求頭攜帶的token
* @return 返回第三方接口的返回信息(一般是code,data等數(shù)據(jù))
* @throws IOException
*/
public static String sendGetHttpUrl(String url,String token) throws IOException {
URL realUrl= new URL(url);
URLConnection connection=realUrl.openConnection();
//設(shè)置請(qǐng)求超時(shí)
connection.setConnectTimeout(6000);
//設(shè)置通用的請(qǐng)求屬性頭
connection.setRequestProperty("accept","*/*");
connection.setRequestProperty("connection","Keep-Alive");
connection.setRequestProperty("user-agent","Mozilla/4.0(compatiable;MSIE 6.0; Windows NT 5.1; sv1)");
if (token != null) {
connection.setRequestProperty("Authorization",token);
}
connection.connect();
//設(shè)置響應(yīng)超時(shí)
connection.setReadTimeout(6000);
//獲取所有響應(yīng)頭字段
Map<String, List<String>> map= connection.getHeaderFields();
for(String key :map.keySet()){
}
//定義bufferedReader字符流讀取URL的響應(yīng)
BufferedReader bred=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line="";
String str="";
while((line=bred.readLine()) !=null){
str+=line;
}
System.out.println(str);
//關(guān)閉 流
bred.close();
return str;
}
2.2.2第二種錯(cuò)誤--不了解第三方接口
-
這個(gè)博主就無(wú)能為力了
只能說(shuō)是多看多學(xué)多寫(xiě)多問(wèn)
-
如果是官方的接口,可以去開(kāi)放平臺(tái)找接口文檔文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-554473.html
2.2.3其他的就不知道咯,如果有也可以留言,一起研究共同進(jìn)步!!
最后在這里謝謝大家的瀏覽。祝大家工作順利,準(zhǔn)時(shí)下班!?。?!文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-554473.html
到了這里,關(guān)于java.io.IOException: Server returned HTTP response code: 400 for URL: http://xxxx的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!