WebRequest立即抛出“无法连接到远程服务器”

时间:2013-06-28 17:39:13

标签: c# windows-8 windows-store-apps

我正在使用WebRequest代码:

bool error = false;
WebException exc = null;
statusText.Text = "Starting";
String urlRequest = "http://****PRIVATER URL****/CC/runCode.php?lang=";
urlRequest += global.language;
urlRequest += "&code=";
urlRequest += Uri.EscapeDataString(global.codeString);
statusText.Text = "Requesting";
webRequest = WebRequest.Create(urlRequest);
webRequest.Proxy = null;
//webRequest.BeginGetResponse(new AsyncCallback(webRequestDone), null);

try
{
    using (resp =  await webRequest.GetResponseAsync())
    {
        //TODO get this working!
    }
}
catch (WebException e)
{
    error = true;
    exc = e;
}
if (error)
{
    await new Windows.UI.Popups.MessageDialog(exc.Message).ShowAsync();
}

无需使用代理即可访问Web服务器。如果我在try catch子句之外得到响应,它运行得很好,如下所示:

bool error = false;
                WebException exc = null;
                statusText.Text = "Starting";
                String urlRequest = "http://****PRIVATE URL****/CC/runCode.php?lang=";
                urlRequest += global.language;
                urlRequest += "&code=";
                urlRequest += Uri.EscapeDataString(global.codeString);
                statusText.Text = "Requesting";
                webRequest = WebRequest.Create(urlRequest);
                webRequest.Proxy = null;
                //webRequest.BeginGetResponse(new AsyncCallback(webRequestDone), null);
                resp = await webRequest.GetResponseAsync();

1 个答案:

答案 0 :(得分:0)

看起来我想通了。事实证明我无法访问专用网络。所以当我在我的本地网络上时总是失败。我必须在app.appxmanifest中的Capabilities下添加它。