UWP 应用无法 post 作为 multipart/form-data 使用摘要身份验证的 Web 服务
UWP App fails to post to a web service as multipart/form-data with digest authentication
尝试 post 使用摘要身份验证作为 multipart/form-data 网络服务时。它因以下错误而失败 'System.Net.CredentialCache' is not supported for 属性 'Credentials'.
使用以下代码发送凭据:
HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) asHttpWebRequest;
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
byte[] formData = GetMultipartFormData(parameters, formDataBoundary);
if (httpWebRequest == null)
{
thrownewNullReferenceException("request is not a http request");
}
// Set up the request properties.
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
httpWebRequest.CookieContainer = newCookieContainer();
var credentialCache = newCredentialCache();
credentialCache.Add(
newUri("http://someurl.com"), // request url's host
"Digest", // authentication type
newNetworkCredential(username, password) // credentials
);
httpWebRequest.Credentials = credentialCache;
我们将服务更改为使用 post 和 application/json 并解决了问题。
var response = await httpClient.PostAsync(requestUrl, new
StringContent(jsonString, Encoding.UTF8, "application/json"));
尝试 post 使用摘要身份验证作为 multipart/form-data 网络服务时。它因以下错误而失败 'System.Net.CredentialCache' is not supported for 属性 'Credentials'.
使用以下代码发送凭据:
HttpWebRequest httpWebRequest = WebRequest.Create(requestUrl) asHttpWebRequest;
string formDataBoundary = String.Format("----------{0:N}", Guid.NewGuid());
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
byte[] formData = GetMultipartFormData(parameters, formDataBoundary);
if (httpWebRequest == null)
{
thrownewNullReferenceException("request is not a http request");
}
// Set up the request properties.
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
httpWebRequest.CookieContainer = newCookieContainer();
var credentialCache = newCredentialCache();
credentialCache.Add(
newUri("http://someurl.com"), // request url's host
"Digest", // authentication type
newNetworkCredential(username, password) // credentials
);
httpWebRequest.Credentials = credentialCache;
我们将服务更改为使用 post 和 application/json 并解决了问题。
var response = await httpClient.PostAsync(requestUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));