求大神告知,java怎么模拟jquery的ajaxsubmit 同时 提交表单和json数据
1个回答
展开全部
URL urlGet = new URL(‘提交表达url’);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 根据自己情况
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//根据自己情况
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();
OutputStream outStrm = httpUrlConnection.getOutputStream();// 获得输出流 通过流写出数据
ObjectOutputStream objOutputStrm = new ObjectOutputStream(outStrm);
objOutputStrm.writeObject(new String("我是测试数据"));
objOutputStm.flush();
objOutputStm.close();
InputStream is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");//message就是提交后的返回结果
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询