asp.net的ajax伺服器端代理

來源:互聯網
上載者:User
在網易上去抓天氣預報,查看www.163.com首頁,天氣預報以xml格式存在在這個位置:
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

 簡潔得可怕啊。

我參照以前的代碼,沒用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像個定時炸彈。。。

測試通過,好晚了,就不再改了。

半拉子工程就是這樣做的。改天再修改。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.