日期:2014-05-19  浏览次数:20687 次

http GET请求
服务端代码如下
@RequestMapping(value = "/data", method = RequestMethod.GET)
public void getList(HttpEntity<String> entity){
  String dataJson = entity.getBody();
String typeStr = entity.getHeaders().getFirst("type");
  /*
  处理
  */
}
请求的URL地址是http://localhost:8080/restful/data
消息为
String reportJson ="json内容......"
String reportType = "类型";


请问客户端的GET请求该如何来构造?



------解决方案--------------------
Java code

String reportJson ="json内容......"
String reportType = "类型";
HttpURLConnection con = 
    (HttpURLConnection)new URL("http://localhost:8080/restful/data").openConnection();
con.setRequestMethod("POST"); //设置为post提交
con.setRequestProperty("type", reportType ); //设置header["类型"]


BufferedWriter bout = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
        bout.write(reportJson);
        bout.flush();
        bout.close();

        // 取得输入流,并使用Reader读取
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                con.getInputStream()));

        String lines;
        while ((lines = reader.readLine()) != null) {
            System.out.println(lines);
        }
        reader.close();