public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
这是我的方法  调用B项目中的一个接口   返回的字符串是B项目的登录界面源码   

解决方案 »

  1.   

    你是请求另一个项目的接口?如果是调用别人的接口,可以这样HttpClient client = HttpClients.createDefault();
       // 要调用的接口方法
       String url = "http://192.168.1.101:8080/getJson";
       HttpPost post = new HttpPost(url);
       JSONObject jsonObject = null;
       try {
          StringEntity s = new StringEntity(date.toString());
          s.setContentEncoding("UTF-8");
          s.setContentType("application/json");
          post.setEntity(s);
          post.addHeader("content-type", "text/xml");
          HttpResponse res = client.execute(post);
          String response1 = EntityUtils.toString(res.getEntity());
          System.out.println(response1);
          if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
             String result = EntityUtils.toString(res.getEntity());// 返回json格式:
             jsonObject = JSONObject.parseObject(result);
          }
       } catch (Exception e) {
          throw new RuntimeException(e);
       }
       return jsonObject;
      

  2.   

    你请求这个接口,验证没有登录,所以会返回登录界面,所以你这边请求到的是登录界面源码。
    你这边请求接口之前先去做token认证,然后在去调用这个接口就可以了。
      

  3.   

    这种一般是调用方式错误或者权限问题,所以没有成功返回你要的数据,返回的是HTML代码。
    返回什么是服务器那边可以设置的,不用太关心,主要是想办法确认正确的调用方式。