Windows-通用 https Post Xamarin 表单
Windows-Universal https Post Xamarin Forms
我想用我的 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 上工作的选项,或者是否有工作的替代方案。
所以我终于找到了解决办法。
根据评论,我尝试了 HttpClient。
但是系统命名空间中的标准 Httpclient 不支持过滤器,我需要通过 SSL 证书问题。
幸运的是,在 Windows.Web.Http 命名空间中,还有另一个支持过滤器的 HttpClient。
答案是功能齐全的 HttpClient Post 调用。
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;
}
我想用我的 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 上工作的选项,或者是否有工作的替代方案。
所以我终于找到了解决办法。 根据评论,我尝试了 HttpClient。 但是系统命名空间中的标准 Httpclient 不支持过滤器,我需要通过 SSL 证书问题。 幸运的是,在 Windows.Web.Http 命名空间中,还有另一个支持过滤器的 HttpClient。 答案是功能齐全的 HttpClient Post 调用。
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;
}