asp.net的ajax服务器端代理
在网易上去抓天气预报,查看www.163.com首页,天气预报以xml格式存在在这个位置:
http://www.163.com/inc/weatherxml/56294.xml
最后的一串数字是省份代码。
参照网易的代码改成自己的(其实就是copy了),在ie6正常,ie7没权限,firefox1.5没权限……
上网查了是跨域问题,解决方法有说“服务器代理”,没找到代码,找到一个asp的:
我参照以前的代码,没用xmlhttp,这样写:
测试通过,好晚了,就不再改了。
半拉子工程就是这样做的。改天再修改。
http://www.163.com/inc/weatherxml/56294.xml
最后的一串数字是省份代码。
参照网易的代码改成自己的(其实就是copy了),在ie6正常,ie7没权限,firefox1.5没权限……
上网查了是跨域问题,解决方法有说“服务器代理”,没找到代码,找到一个asp的:
1
2<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
3<%
4Function send_request(url)
5Set Retrieval = CreateObject("Microsoft.XMLHTTP")
6With Retrieval
7 .Open "Get", url, False, "", ""
8 .Send
9 send_request = .ResponseBody
10End With
11Response.ContentType="text/xml"
12Set Retrieval = Nothing
13End Function
14
15url=Request("url")
16Response.BinaryWrite send_request(url)
17Response.Flush
18%>
19
简洁得可怕啊。2<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
3<%
4Function send_request(url)
5Set Retrieval = CreateObject("Microsoft.XMLHTTP")
6With Retrieval
7 .Open "Get", url, False, "", ""
8 .Send
9 send_request = .ResponseBody
10End With
11Response.ContentType="text/xml"
12Set Retrieval = Nothing
13End Function
14
15url=Request("url")
16Response.BinaryWrite send_request(url)
17Response.Flush
18%>
19
我参照以前的代码,没用xmlhttp,这样写:
1 <%@ OutputCache Duration="600" VaryByParam="*" %>
2 <script language="C#" runat="server">
3 override protected void OnInit(EventArgs e)
4 {
5 string strUrl, strXml;
6 strUrl = Server.UrlDecode(Request.QueryString["url"]);
7 strXml = this.GetUrlToHtml(strUrl);
8
9 Response.ContentType = "text/xml";
10 Response.BinaryWrite(System.Text.Encoding.Default.GetBytes(strXml));
11 Response.Flush();
12
13 }
14
15 public string GetUrlToHtml(string url)
16 {
17 if (url=="" || url==null) return "";
18
19 try
20 {
21 Uri uri = new Uri(url);
22 System.Net.WebRequest wReq = System.Net.WebRequest.CreateDefault(uri);
23 System.Net.WebResponse wResp =wReq.GetResponse();
24 System.IO.Stream respStream = wResp.GetResponseStream();
25 System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
26 string strHtml = reader.ReadToEnd();
27 reader.Close();
28 return strHtml;
29 }
30 catch
31 {
32 return "";
33 }
34 }
35 </script>
觉得复杂了。而且那个Encoding像个定时炸弹。。。2 <script language="C#" runat="server">
3 override protected void OnInit(EventArgs e)
4 {
5 string strUrl, strXml;
6 strUrl = Server.UrlDecode(Request.QueryString["url"]);
7 strXml = this.GetUrlToHtml(strUrl);
8
9 Response.ContentType = "text/xml";
10 Response.BinaryWrite(System.Text.Encoding.Default.GetBytes(strXml));
11 Response.Flush();
12
13 }
14
15 public string GetUrlToHtml(string url)
16 {
17 if (url=="" || url==null) return "";
18
19 try
20 {
21 Uri uri = new Uri(url);
22 System.Net.WebRequest wReq = System.Net.WebRequest.CreateDefault(uri);
23 System.Net.WebResponse wResp =wReq.GetResponse();
24 System.IO.Stream respStream = wResp.GetResponseStream();
25 System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
26 string strHtml = reader.ReadToEnd();
27 reader.Close();
28 return strHtml;
29 }
30 catch
31 {
32 return "";
33 }
34 }
35 </script>
测试通过,好晚了,就不再改了。
半拉子工程就是这样做的。改天再修改。