1. 在做项目时,初次调用.net接口,利用ajax有跨域的问题,利用后台方法解决。
主要是利用 DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
out.writeBytes(obj.toString());
进行参数的写入。
@RequestMapping("/updateBackUserPass")
public String updateBackUserPass(HttpServletRequest request) throws IOException {String userName=request.getParameter("Username");
String rePass=request.getParameter("Pwd");
String OldPwd=request.getParameter("OldPwd");
OldPwd="123456";
StringBuffer sb = new StringBuffer("");
try {
//创建连接
URL url = new URL("http://168.160.200.188:8081/ModifyPasHandler.ashx");
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
//POST请求
DataOutputStream out = new DataOutputStream(
connection.getOutputStream());
JSONObject obj = new JSONObject();
obj.put("UserName",userName);
obj.put("Pwd",rePass);
/* obj.put("OldPwd",OldPwd);*/
System.out.println(obj.toString());
out.writeBytes(obj.toString());
out.flush();
out.close();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb.toString());
reader.close();
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}
2.第二中方式可以利用拼出Url串进行传值访问并返回,可以进行进行接收返回值