C# windows 到 C# phone windows

C# windows to C# phone windows

这与其说是帮助,不如说是一项需要询问的任务,但是...经过 1 天的尝试后,我无法让它工作。 这是我的代码

private string _InetReadEx(string sUrl)
        {

            HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(sUrl);
            try
            {
                webReq.CookieContainer = new CookieContainer();
                webReq.Method = "GET";
                using (WebResponse response = webReq.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        StreamReader reader = new StreamReader(stream);
                        aRet = reader.ReadToEnd();
                        return aRet;
                    }
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }

这段代码的作用很简单。它returns 一个服务器的来源。 所以这是我的愚蠢问题;我怎样才能在 C# windows phone(windows phone 8.1/8) 上做类似的事情?

提前谢谢你, 玛丽亚

我建议你使用 HttpClient I have used it both in Windows / Win Phone projects and works like a charm, have a look at this

试试这个 -

webReq.BeginGetResponse(GetResponseCallback, request);

void GetResponseCallback(IAsyncResult result)
{
    HttpWebRequest request = result.AsyncState as HttpWebRequest;
    if (request != null)
    {
       try
       {
        WebResponse response = request.EndGetResponse(result);
        // use response 
       }
       catch (WebException e)
       {
        return;
       }
   }
}