在Java程序中可以直接调用jsp、Struts Action等网络资源,示例代码: Java代码 1.String title = "这是标题"; 2.String url_str = "http://127.0.0.1:8080/struts/login.do?title="; 3.URL url = new URL(url_str+title); 4.HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 5.connection.connect(); 6.int code = connection.getResponseCode(); 7.log.info("code========" + code); String title = "这是标题"; String url_str = "http://127.0.0.1:8080/struts/login.do?title="; URL url = new URL(url_str+title); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); int code = connection.getResponseCode(); log.info("code========" + code);注意:若不调用如下代码,则不能调用成功 Java代码 1.int code = connection.getResponseCode(); int code = connection.getResponseCode(); 转载资料1: Java代码 1.//java如何通过url调用远程接口并读取返回信息? 2. String ticket = "";//登录凭证 3. String url_str = "http://www.sina.com.cn?ticket=";//获取用户认证的帐号URL 4. String ticket_url = url_str + ticket; 5. URL url = new URL(ticket_url); 6. HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 7. connection.connect(); 8. 9. int code = connection.getResponseCode(); 10. if (code == 404) { 11. throw new Exception("认证无效,找不到此次认证的会话信息!"); 12. } 13. if (code == 500) { 14. throw new Exception("认证服务器发生内部错误!"); 15. } 16. if (code != 200) { 17. throw new Exception("发生其它错误,认证服务器返回 " + code); 18. } 19. InputStream is = connection.getInputStream(); 20. byte[] response = new byte[is.available()]; 21. is.read(response); 22. is.close(); 23. if (response == null || response.length == 0) { 24. throw new Exception("认证无效,找不到此次认证的会话信息!"); 25. } 26. String userId = new String(response, "GBK"); 27. System.out.println(userId); //java如何通过url调用远程接口并读取返回信息? String ticket = "";//登录凭证 String url_str = "http://www.sina.com.cn?ticket=";//获取用户认证的帐号URL String ticket_url = url_str + ticket; URL url = new URL(ticket_url); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); int code = connection.getResponseCode(); if (code == 404) { throw new Exception("认证无效,找不到此次认证的会话信息!"); } if (code == 500) { throw new Exception("认证服务器发生内部错误!"); } if (code != 200) { throw new Exception("发生其它错误,认证服务器返回 " + code); } InputStream is = connection.getInputStream(); byte[] response = new byte[is.available()]; is.read(response); is.close(); if (response == null || response.length == 0) { throw new Exception("认证无效,找不到此次认证的会话信息!"); } String userId = new String(response, "GBK"); System.out.println(userId); 转载资料2: Java代码 1. URL u = new URL( url ); 2. URLConnection uc = u.openConnection(); 3. HttpURLConnection httpUC = (HttpURLConnection)uc; 4.// 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false; 5. httpUC.setDoOutput( true ); 6.// Post 请求不能使用缓存 7. httpUC.setUseCaches(false); 8. 9.// 增加更多的请求头信息 10. Iterator it = requestHeads.keySet().iterator(); 11. while( it.hasNext() ){ 12. String name = it.next(); String value = requestHeads.get( name ); 13. httpUC.addRequestProperty( name , value ); 14. } 15. 16.// 设定传送的内容类型是可序列化的java对象 17.// (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException) 18. httpUC.setRequestProperty("Content-type", "application/x-java-serialized-object"); 19.// 设定请求的方法为"POST",默认是GET 20. httpUC.setRequestMethod("POST"); 21. 22.// 连接,上面的配置必须要在connect之前完成. 不知道是否要连接池??? rhwang 2009-02-27 23. httpUC.connect(); 转自:http://xucunliang.iteye.com/blog/788838
java action转action_java内部调用action(转)
最新推荐文章于 2021-03-06 07:02:36 发布