【问题标题】:Windows-Universal https Post Xamarin FormsWindows 通用 https 发布 Xamarin 表单
【发布时间】:2016-02-16 16:01:32
【问题描述】:

我想使用我的 Windows 通用应用程序发出 Json Post 请求。 我可以在我的 Android 和 IOS 中使用它。

   public String DoServiceCall()
    {
        var request = (HttpWebRequest)WebRequest.Create(string.Format("{2}/{0}/{0}ServiceJson.svc/{1}", "Authentication", "Authenticate", "https://....."));

        using (MemoryStream ms = new MemoryStream())
        {
            // create the request object
            string requestString = JSONRequest;
            byte[] requestData = Encoding.UTF8.GetBytes(requestString);
            request.Method = "POST";
            request.ContentType = "application/json; charset=UTF-8";
            request.ContentLength = requestData.Length;
            request.AllowAutoRedirect = false;

            // add known cookies to request (needed for authentication)
            CookieContainer requestCookies = new CookieContainer();
            foreach (Cookie knownCookie in this._cookieCollection)
            {
                requestCookies.Add( knownCookie);
            }
            request.CookieContainer = requestCookies;

            //For getting rid of the https Problem
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            using (Stream stream =  request.GetRequestStream())
            {
                stream.Write(requestData, 0, requestData.Length);
            }
            // get response data
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

            string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            return (responseString);
        }
    }

问题是 Windows Universal 不支持。

request.ContentLength = requestData.Length;
request.AllowAutoRedirect = false;
requestCookies.Add( knownCookie); //with only one Argument
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

也不支持。

request.GetRequestStream())
(HttpWebResponse) request.GetResponse();

但是我可以用异步修复

await request.GetRequestStreamAsync())
(HttpWebResponse)await request.GetResponseAsync();

但如果没有那 4 行,我无法让它在 Windows 上运行。 我只是没有得到任何回应。 是否有让它在 Windows 10 上运行的选项,或者是否有可行的替代方案。

【问题讨论】:

  • 试试this答案。
  • 试过了,还是没有答案。我想我可能是自定义 SSL Zertifikat 的问题。
  • 你没有,检查 HttpClient 类,它可以满足你的所有需求。

标签: c# https xamarin win-universal-app xamarin.forms


【解决方案1】:

所以我终于找到了解决方案。 根据评论,我尝试了 HttpClient。 但是系统命名空间中的标准 Httpclient 不支持过滤器,我需要通过 SSL 证书问题。 幸运的是,在 Windows.Web.Http 命名空间中,还有另一个支持过滤器的 HttpClient。 答案是功能齐全的 HttpClient Post Call。

public async void testcallwind()
    {
        List<HttpCookie> cookieCollection = new List<HttpCookie>();
        HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
        HttpClient httpClient;
        filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);//Allow untrusted CA's 
        filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
        // add known cookies to request (needed for authentication)
        foreach (HttpCookie knownCookie in cookieCollection)
        {
            filter.CookieManager.SetCookie(knownCookie);
        }
        httpClient = new HttpClient(filter);
        string resourceAddress = string.Format("https://...");
        string requestString = "{\"request\":{\"CallerState\":null,...."}}";
        byte[] requestData = Encoding.UTF8.GetBytes(requestString);        
        UnicodeEncoding en = new UnicodeEncoding();

        IHttpContent content = new HttpStringContent(requestString, 0, "application/json");
        Uri postBody = new Uri(resourceAddress);            

        var response = await httpClient.PostAsync(postBody, content);
        httpClient.Dispose();
        var test = response.Content;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多