展开全部
HttpURLConnection 请求中 参数中如果有 空格,请求则会 505错误
解决: 需要对有空格的参数 做URL编码32313133353236313431303231363533e58685e5aeb931333339653664处理。import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import sun.net.www.protocol.http.HttpURLConnection;
import com.alibaba.fastjson.JSONObject;
public class SingleTableRestClient
{
private static final String targetURL = "http://localhost:8080/agd-restful/services/restful/QueryService/queryData/*?queryParam=";
public static void main(String[] args)
{
JSONObject obj = new JSONObject();
obj.put("XM", "匡匡");
obj.put("BIRTHDAY", getURLEncoder("1988-01-01 00:00:00,1988-12-30 00:00:00"));
String urls = targetURL + obj.toString();
requestRestServer(urls);
}
public static JSONObject requestRestServer(String url)
{
JSONObject obj = new JSONObject();
try
{
URL restServiceURL = new URL(url);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Accept-Charset", "UTF-8");
httpConnection.setRequestProperty("contentType", "UTF-8");
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream()),"utf-8"));
String output = "";
String result = "";
System.out.println("Output from Server: \n");
while ((output = responseBuffer.readLine()) != null) {
//System.out.println(output);
result = output;
}
obj = JSONObject.parseObject(result);
System.out.println(obj.toString());
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return obj;
}
@SuppressWarnings("deprecation")
private static String getURLEncoder(String dest)
{
return URLEncoder.encode(dest);
}
}
修改后 正常ok