不同项目,如何java模拟ajax访问另一个项目的controller
直接上码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
package com.ultrapower.zq.iscloud.web.boc.api.utils; /** * create by liujie */ import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.printwriter; import java.net.url; import java.net.urlconnection; import java.util.list; import java.util.map; import org.json.jsonobject; import org.jsoup.jsoup; import org.jsoup.nodes.document; public class httputils { /** * 向指定url发送get方法的请求 * @param url * 发送请求的url * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return url 所代表远程资源的响应结果 */ 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; } /** * 向指定 url 发送post方法的请求 * @param url * 发送请求的 url * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static string sendpost(string url) { printwriter out = null ; bufferedreader in = null ; string result = "" ; try { url realurl = new url(url); // 打开和url之间的连接 urlconnection conn = realurl.openconnection(); // 设置通用的请求属性 conn.setrequestproperty( "accept" , "*/*" ); // conn.setrequestproperty("connection", "keep-alive"); conn.setrequestproperty( "content-type" , "application/json; charset=utf-8" ); // conn.setrequestproperty("user-agent", // "mozilla/4.0 (compatible; msie 6.0; windows nt 5.1;sv1)"); // 发送post请求必须设置如下两行 conn.setdooutput( true ); conn.setdoinput( true ); // 获取urlconnection对象对应的输出流 out = new printwriter(conn.getoutputstream()); // 发送请求参数 // out.print(param); // flush输出流的缓冲 out.flush(); // 定义bufferedreader输入流来读取url的响应 in = new bufferedreader( new inputstreamreader(conn.getinputstream())); string line; while ((line = in.readline()) != null ) { result += line; } } catch (exception e) { system.out.println( "发送 post 请求出现异常!" +e); e.printstacktrace(); } //使用finally块来关闭输出流、输入流 finally { try { if (out!= null ){ out.close(); } if (in!= null ){ in.close(); } } catch (ioexception ex){ ex.printstacktrace(); } } return result; } public static void main(string[] args) { // document doc = jsoup.parse("http://xxx/xxxx?entname=餐饮管理有限公司"); // system.out.println(doc); //发送 get 请求 string s=httputils.sendpost( "http://xxxxxxxx?entname=餐饮管理有限公司" ); system.out.println(s); //发送 post 请求 // string sr=httputils.sendpost("http://localhost:6144/home/requestpoststring", "key=123&v=456"); // system.out.println(sr); } } |
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/qq_26562641/article/details/73549788
查看更多关于java模拟ajax访问另一个项目的controller代码实例的详细内容...