我的客户端需要从公司的server上获取数据,他们提供的接口是 发送post到http://aqueduc.kelcode.com/proc/gw.php
后面跟上一些参数,然后就可以得到我想要的json数据。我先通过在网页上直接用ajax发送post到公司的server,但是失败。在网上查的结果是js跨域访问危险地,所以不支持(IE在提示风险并继续运行的情况下可以用)。从网上得到的建议是:我可以通过jsp作为代理,从浏览器发post给proxy.jsp,再由proxy.jsp从本地server把想要发送的post请求发给公司的server,再把数据传回客户端。下面是我改写的代码。但总是得到http 500的错误。我不太会web编程。jsp也是现学的,连debug都不知道怎么弄。是不是有什么配置上的问题,还是我的代码有错误,或者是思路都是不对的。非常着急,再做不出来就要被炒了。请求大家帮忙。这个用单纯的java写的代码给公司server发送post是可以得到正确的结果的,我已经测试过了。<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function loadJson()
{
var xmlhttp = false;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (XMLHttpRequest.overrideMimeType)
{
XMLHttpRequest.overrideMimeType("text/xml");
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xmlhttp)
{
window.alert("不能创建对象");
return false;
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
else
document.getElementById("myDiv").innerHTML=xmlhttp.status+"-"+xmlhttp.readyState;
}
xmlhttp.open("POST","proxy.jsp",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("url=http://aqueduc.kelcode.com/proc/gw.php&requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
}
</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<button type="button" onclick="loadJson()">请求数据</button>
</body>
</html>proxy.jsp<%@ page import="java.net.*,java.util.*,java.lang.*,java.io.*"%>
<%@ page contentType="text/xml;charset=gb2312"%>
<%
String url = null;
StringBuffer params = new StringBuffer();
Enumeration enu = request.getParameterNames();
int total = 0;
while (enu.hasMoreElements()) {
String paramName=(String)enu.nextElement();
if(paramName.equals("url")){
url=request.getParameter(paramName);
}else{
if(total==0){
params.append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
} else {
params.append("&").append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
}
++total;
}
}
String para = params.toString();
//out.println(url);
if(url != null){
// 使用GET方式向目的服务器发送请求
URL connect = new URL(url.toString());
URLConnection connection = connect.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
connection.connect();
DataOutputStream os = new DataOutputStream(connnection.getOutputStream());
os.write("requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
os.flush();
os.close();BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = reader.readLine()) != null){
out.println(line);
}
reader.close();
}
%>
后面跟上一些参数,然后就可以得到我想要的json数据。我先通过在网页上直接用ajax发送post到公司的server,但是失败。在网上查的结果是js跨域访问危险地,所以不支持(IE在提示风险并继续运行的情况下可以用)。从网上得到的建议是:我可以通过jsp作为代理,从浏览器发post给proxy.jsp,再由proxy.jsp从本地server把想要发送的post请求发给公司的server,再把数据传回客户端。下面是我改写的代码。但总是得到http 500的错误。我不太会web编程。jsp也是现学的,连debug都不知道怎么弄。是不是有什么配置上的问题,还是我的代码有错误,或者是思路都是不对的。非常着急,再做不出来就要被炒了。请求大家帮忙。这个用单纯的java写的代码给公司server发送post是可以得到正确的结果的,我已经测试过了。<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
function loadJson()
{
var xmlhttp = false;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
if (XMLHttpRequest.overrideMimeType)
{
XMLHttpRequest.overrideMimeType("text/xml");
}
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xmlhttp)
{
window.alert("不能创建对象");
return false;
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
else
document.getElementById("myDiv").innerHTML=xmlhttp.status+"-"+xmlhttp.readyState;
}
xmlhttp.open("POST","proxy.jsp",true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlhttp.send("url=http://aqueduc.kelcode.com/proc/gw.php&requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
}
</script><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<button type="button" onclick="loadJson()">请求数据</button>
</body>
</html>proxy.jsp<%@ page import="java.net.*,java.util.*,java.lang.*,java.io.*"%>
<%@ page contentType="text/xml;charset=gb2312"%>
<%
String url = null;
StringBuffer params = new StringBuffer();
Enumeration enu = request.getParameterNames();
int total = 0;
while (enu.hasMoreElements()) {
String paramName=(String)enu.nextElement();
if(paramName.equals("url")){
url=request.getParameter(paramName);
}else{
if(total==0){
params.append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
} else {
params.append("&").append(paramName).append("=").append(URLEncoder.encode(request.getParameter(paramName), "UTF-8"));
}
++total;
}
}
String para = params.toString();
//out.println(url);
if(url != null){
// 使用GET方式向目的服务器发送请求
URL connect = new URL(url.toString());
URLConnection connection = connect.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type","application/x-www-form-urlencoded");
connection.connect();
DataOutputStream os = new DataOutputStream(connnection.getOutputStream());
os.write("requestName=getIntervention&uid=UID_GATEWAY&startDate=1292223600&endDate=1408330000");
os.flush();
os.close();BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = reader.readLine()) != null){
out.println(line);
}
reader.close();
}
%>
解决方案 »
- 在用户未登录执行某些操作(如:下载)时,弹窗要求用户登陆或注册时的一个小问题
- 子窗体刷新父窗体
- 浏览器地址栏输入URL后能跳转到servlet吗?在线等,,求高手
- 求“Unable to compile class for JSP”解决办法
- tomcat被黑 对方留下个fexcep目录 有高手知道是干嘛的不
- js关于取得文本框值并进行求和的问题
- jaas 程序入口的问题
- 怎样给<tag-file>添加attribute属性,可以在网页中使用此标签提示属性
- 打包问题!
- IIS和Resin整合的问题
- ResultSet updateDate問題
- 请问甲骨文收购Sun之后看,过去的标签库的地址变到哪里去了?
这个问题就像男人不能怀孕一样
好像公司那边的服务器需要发post过去,我了解的jsonp只能发送get,不知道是不是我理解错了。
大概的给你描述下。首先 分2种情况
1. 对方公司 返回的 json 数据 如果是 这个样子的
retrun( {name:'复杂吗?',value:'不复杂吗?'});
那么恭喜你。很好做。。
return 是函数名 ( *** ) 括号里的相当于 是 参数你用动态的javascript 来调用 9行了 例如
<javascript src = ' 这里写 上已经拼接好的 请求URL'></javascript>
动态创建 javascript 标签 我9不多说了 很多来着 问度娘
让后在 本页面 js 块里面 写上 function return(json){ ... } 在这个函数中9能调用 传过来的值了2. 如果不是 那么还是恭喜你 这个方法更复杂了 做法很多 你上述的做法 9行。还有什么 IFRAME 什么的
这个不好表述 而且问题特多 不是一时半会能说清楚 所以你只能问 度娘。
<script></script>
直接就是json对象这样的数据
{"nbreContacts":0,"contactId":null,"lastName":null,"firstName":null,"mobilePhone":null,"status":"OK","size":101,"sizeR":null}我如果想知道又什么异常是否要在myeclipse下跑一下,然后大家就可以更清楚一些呢。我去试一下吧。
另外webapps/test文件夹下是不是必须要再建一个WEB-INF文件夹,里面有class和.xml的文件?
看来我还是不能用记事本去写代码,水平不够啊。
谢谢各位的帮助!