HTTP500内部服务器错误
- 1.問題描述
- 2.問題解決
- 3.HTTP請(qǐng)求代碼
1.問題描述
1.使用http方式請(qǐng)求對(duì)方服務(wù)器,返回JSON格式數(shù)據(jù)
2.由于是測(cè)試環(huán)境,網(wǎng)段不是同一個(gè),對(duì)方服務(wù)器網(wǎng)段做了代理,我們能ping通,也能telnet
3.使用瀏覽器訪問能正常返回JSON格式數(shù)據(jù)
4.用代碼解析http請(qǐng)求一直報(bào)500錯(cuò)誤,對(duì)方覺得是我們代碼問題,但是我們用代碼解析公網(wǎng)上的一些請(qǐng)求是正常的;而且我們將代碼發(fā)給對(duì)方,讓對(duì)方解析,也是正常的
2.問題解決
由于我們本地看不出什么問題,我們用代碼請(qǐng)求,讓對(duì)方服務(wù)器進(jìn)行抓包調(diào)試。
1.對(duì)方服務(wù)器讓我們將請(qǐng)求的Content-Type改為application/json,請(qǐng)求仍然有問題
Error-Reason:CHARACTER:error="json"; error_description*=UTF-8''Expected one of: <<{,[>> but got: <<C>>
2.對(duì)方查了下,發(fā)現(xiàn)服務(wù)器Content-Type填什么都報(bào)錯(cuò),application/x-www-form-urlencoded、application/json都報(bào)錯(cuò),只有不填才能正確,空串也不行;我們就說http請(qǐng)求頭有個(gè)默認(rèn)值就是x-www-form-urlencoded,對(duì)方服務(wù)器端改了這個(gè)Content-Type的請(qǐng)求頭,我們代碼就能正常訪問到JSON數(shù)據(jù)了
3.HTTP請(qǐng)求代碼
public static String sendGet(String url, String param) { String result = "";BufferedReader in = null;try {String urlName = url + "?" + param;URL realUrl = new URL(urlName);// 打開和URL之間的連接URLConnection conn = realUrl.openConnection();// 設(shè)置通用的請(qǐng)求屬性conn.setRequestProperty("accept", "*/*");conn.setRequestProperty("connection", "Keep-Alive");conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");// 建立實(shí)際的連接conn.connect();// 獲取所有響應(yīng)頭字段Map<String, List<String>> map = conn.getHeaderFields();// 遍歷所有的響應(yīng)頭字段for (String key : map.keySet()) {System.out.println(key + "--->" + map.get(key));}// 定義BufferedReader輸入流來讀取URL的響應(yīng)in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));String line;while ((line = in.readLine()) != null) {result += line;}} catch (Exception e) {System.out.println("發(fā)送GET請(qǐng)求出現(xiàn)異常!" + e);e.printStackTrace();}// 使用finally塊來關(guān)閉輸入流finally {try {if (in != null) {in.close();}} catch (IOException ex) {ex.printStackTrace();}}return result;}總結(jié)
以上是生活随笔為你收集整理的HTTP500内部服务器错误的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二十三)美萍酒店管理系统:系统维护_系
- 下一篇: linux 锐捷 dns,锐捷Linux