我想用我的Windows Universal App制作一个Json Post Request。 我在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上运行,或者是否有工作替代方案。
答案 0 :(得分:1)
所以我终于找到了解决方案。 基于评论,我尝试了HttpClient。 但系统命名空间中的Stanard 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;
}