简体   繁体   中英

windows store app 8.1 httpclient request failes in local private network

http request to receive json fails in private network or even in same system server set with xamp it is working fine with public networks over internet.

i have given internet client&server , private networks capabilities in appxmanifest

code:

public static async Task<string> GetStringfromUrl(string url)
    {

        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.Credentials = new System.Net.NetworkCredential(GlobalVariables.Web_AccountName,GlobalVariables.Web_AccountPassword);

        HttpClient httpClient = new HttpClient(httpClientHandler);



        try
        {
            string responseBodyAsText;

            HttpResponseMessage response = await httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();
            responseBodyAsText = await response.Content.ReadAsStringAsync();

            return responseBodyAsText;

        }

        catch(Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Message +"/n" + ex.InnerException);
            return null;
        }
    }

in private network it shows authorization exception ,in same system localhost it doesnt pass the line of code.while works all fine in public internet please help

Information upon which solution to try is based

"Using network loopback in side-loaded Windows Store apps" http://msdn.microsoft.com/en-us/library/windows/apps/dn640582.aspx @ section "Configuring the firewall"

CheckNetIsolation utility

Windows Firewall also blocks loopback connections for all Windows Store apps by default. A device admin can enable loopback for a Windows Store app using the CheckNetIsolation.exe tool. This tool is available on the command line in any Windows 8.1 installation.

Solution to try

Use @ command prompt

checknetisolation loopbackexempt -a -n=<package-family-name>

Where

-a

stands for add

-n

stands for AppContainer Name or Package Family Name (which we care in this case) and

<package-family-name>

can be found @ packaging.appmanifest @ packaging

CheckNetIsolation.exe usage

CheckNetIsolation LoopbackExempt [operation] [-n=] [-p=] List of operations: -a -d -c -s List of arguments: -n= -p= -?

Context within the issue was encountered in my case

During testing a Windows (Store App) Portable Library via a Windows (Store App) Unit Test which accessed a localhost self hosting WebAPI setup-ed by a parallel running console app.

Notes

I too tried the Capabilities, you reffer to, to no avail and after fixing the problem I actually reverted back to original configuration which did not use any of those 2 additional ones (Internet (Client & Server), Private Networks (Client & Server)) to see if they mattered and the solution above still worked, probably because those have to do with having your app behave as a server and handle requests.

Let me know if this resolves the issue and if yes don't forget to accept the answer!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM