C#UWP UploadOperation BackgroundUploader无法找到响应

时间:2019-06-13 10:22:21

标签: c# uwp

                string boundray = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                string url = HttpDomainHandling(currentUser.DomainURL) + API + "&is_multi_part_upload=true";
                string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n" + "Content-Type: {3}\r\n\r\n", boundray, "file", file.Name, "application/octet-stream");
                string footer = string.Format("\r\n--{0}--\r\n", boundray);

                Stream headerStream = GenerateStreamFromString(header);
                Stream footerStream = GenerateStreamFromString(footer);
                Stream dataStream = await sfile.OpenStreamForReadAsync();

                MemoryStream fileDataStream = new MemoryStream();
                await headerStream.CopyToAsync(fileDataStream);
                await dataStream.CopyToAsync(fileDataStream);
                await footerStream.CopyToAsync(fileDataStream);
                fileDataStream.Position = 0;

                IInputStream stream = fileDataStream.AsInputStream();

                BackgroundUploader backgroundUploader = new BackgroundUploader();
                backgroundUploader.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundray);
                backgroundUploader.SetRequestHeader("Cookie", Constants.FELIXSESSIONID + "=" + currentUser.SessionID);
                backgroundUploader.Method = "POST";

                UploadOperation uploadOpration = await backgroundUploader.CreateUploadFromStreamAsync(new Uri(url), stream);

                await Task.Factory.StartNew(() => CheckUploadStatus(uploadOpration, progressEvent, cts));
                var result = await uploadOpration.StartAsync();
                ResponseInformation info = uploadOpration.GetResponseInformation();
                return info;

在结果和响应信息中找不到json响应..我在哪里可以得到此响应...

我正在尝试将文件上传到我的服务器..并且它以json格式返回上传的数据..

1 个答案:

答案 0 :(得分:0)

最后得到答案

  • BackgroundUploader用于您要在IP或某个地址上上传内容,并且您不希望来自服务器的响应字符串...

  • BackgroundUploader可以仅返回状态码并成功返回错误消息信息

如果您要上传内容,并且想要响应字符串示例JSON或XML ... 需要使用UWP HTTPCLIENT

//Create an HTTP client object
Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();

//Add a user-agent header to the GET request. 
var headers = httpClient.DefaultRequestHeaders;

//The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
//especially if the header value is coming from user input.
string header = "ie";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

Uri requestUri = new Uri("http://www.contoso.com");

//Send the GET request asynchronously and retrieve the response as a string.
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";

try
{
    //Send the GET request
    httpResponse = await httpClient.GetAsync(requestUri);
    httpResponse.EnsureSuccessStatusCode();
    httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
    httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}