简体   繁体   中英

Access to web service wit IPv4 and IPv6 for iOS app created in Visual Studio with Xamarin

I create iOS app in VS with Xamarin, that get information (text/images/etc) from server. First I made access to the server through domain name (like http://domainname.com/getinfo?params=xyz ). This works both for IPv4 and IPv6. But now I have to use access to the server through IP number. Server has both IPv4 number and IPv6 number. To get information I use HttpClient with query string like " http://xxx.xxx.xxx.xxx/getinfo?params=xyz " This works when device connected to network through IPv4. But when device connected to network through IPv6, it doesn't work (as iTunes connect said). Can anybody help me with this?

UPDATED

Example of code:

public class CampaignData
{
    public int _campaignId;
    public string _campaignName;
    public string _campaignDescription;
    public CampaignData(int campaignId, string campaignName, string campaignDescription){
        _campaignId=_campaignId;
        _campaignName=campaignName;
        _campaignDescription=campaignDescription;
    }
}
public class DataService
{
    private static string _requestUri = "http://xxx.xxx.xxx.xxx/getinfo/";
    private static string _requestUriV6 = "http://xxxx:xxxx:x:xxx:xxxx:xxxx:xxxx:xxxx/getinfo/";
    public static async Task<List<CampaignData>> GetCampaignsList(string lang)
    {
        string queryString = _requestUri + "get_campaigns_list.php?lang="+lang;
        HttpClient client = new HttpClient();
        var response = await client.GetAsync(queryString);
        dynamic data = null;
        if (response != null)
        {
            List<CampaignData> campaignsList = new List<CampaignData>();
            string json = response.Content.ReadAsStringAsync().Result;
            data = JsonConvert.DeserializeObject(json);
            if (data["status"] == "success")
            {
                foreach (var campaign in data["campaigns"])
                {
                    int id = campaign["idx"];
                    string description = campaign["description"];
                    string name = campaign["description"];
                    campaignsList.Add(new CampaignData(id, name, description));
                }
                return campaignsList;
            }
            else
            {
                return null;
            }
        }
        return null;
    }
}

IPv6 address literals must be encoded with [ and ] around them:

private static string _requestUriV6 = "http://[2001:db8::1]/getinfo/";

Port numbers optionally go after the ] :

private static string _requestUriV6 = "http://[2001:db8::1]:80/getinfo/";

Do warn your client that on IPv6 networks that only support NAT64 and not native IPv6 (like the test-network that Apple devices provide). Your current code may still get rejected because of that. They do mention how you could deal with address literals but explicitly recommend against it.

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