ASP.NET无法从外部URL获取数据

时间:2013-10-17 13:16:21

标签: c# asp.net httpwebrequest

我已经尝试了一切,我猜我有一个代码块。

我正在使用这个aspx文件:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Uri uri = new Uri("http://www.amazon.com/");
        string data = "";
        if (uri.Scheme == Uri.UriSchemeHttp)
        {
        try{
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentLength = data.Length;
            request.ContentType = "application/x-www-form-urlencoded";

            StreamWriter writer = new StreamWriter(request.GetRequestStream());
            writer.Write(data);
            writer.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();
            response.Close();

            this.Panel1.GroupingText = tmp;
            }
            catch(WebException ex){
            Response.Write(ex.Status);
            }
        }
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server"
            Height="355px" Width="480px" ScrollBars="Auto">
        </asp:Panel>

    </div>
    </form>
</body>
</html>

我的iis可能有什么问题。我在端口80使用localhost。

也许我需要提供更多数据,如果有,请告诉我。

我希望你能帮助我。

提前致谢

0 个答案:

没有答案