怎么做HTTP进入silverlight(Winphone)

时间:2012-04-02 13:06:51

标签: c# .net silverlight windows-phone

如何进行HTTP获取Silverlight(Winphone)?

我的代码无效(抛出异常)

我得到以下例外情况:

A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.dll
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.dll
A first chance exception of type 'System.MissingMethodException' occurred in mscorlib.dll
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.dll

以下是代码:

    public Boolean getAnnouncements()
    {
        try
        {
            string url = "https://server.james-bennet.com:8443/BookingSystem/Announcement";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.Accept = "application/json";
            request.ContentType = "application/json; charset=utf-8";

            HttpWebResponse response = request.BeginGetResponse(new AsyncCallback(ReadCallback), request) as HttpWebResponse;
            return true;
        }
        catch (Exception e)
        {
            System.Console.WriteLine(e.ToString());
            return false;
        }
    }

    private void ReadCallback(IAsyncResult asynchronousResult)
    {
        try
        {
        HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
        HttpWebResponse response =(HttpWebResponse)request.EndGetResponse(asynchronousResult);

        using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
        {
            string resultString = streamReader1.ReadToEnd();
            System.Console.WriteLine(resultString);
        }
                    }
        catch (Exception e)
        {
            System.Console.WriteLine(e.ToString());
        }
    }

1 个答案:

答案 0 :(得分:0)

你确定你有

<Capability Name="ID_CAP_NETWORKING"/>

在WMAppManifest.xml文件中配置?