Asp.net ajax server proxy

Source: Internet
Author: User
Capture the weather forecast on Netease and view the www.163.com homepage. The weather forecast is stored in xml format:
Http://www.163.com/inc/weatherxml/56294.xml
The last string of numbers is the province code.

Refer to Netease's code to change it to your own (in fact, it is copy). in ie6, it is normal, ie7 has no permission, firefox1.5 has no permission ......
The cross-origin problem is found online. The solution is "server proxy". The code is not found, and an asp:

1
2 <% @ LANGUAGE = "VBSCRIPT" CODEPAGE = "936" %>
3 <%
4 Function send_request (url)
5 Set Retrieval = CreateObject ("Microsoft. XMLHTTP ")
6 With Retrieval
7. Open "Get", url, False ,"",""
8. Send
9 send_request =. ResponseBody
10End
11Response. ContentType = "text/xml"
12 Set Retrieval = Nothing
13End Function
14
15url = Request ("url ")
16Response. BinaryWrite send_request (url)
17Response. Flush
18%>
19

Concise and terrible.

I did not use xmlhttp by referring to the previous code, so I wrote: 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>

I think it is complicated. And the Encoding is like a time bomb...

The test passed. It's too late to change.

This is what the half-pull project does. Modify the settings on another day.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.