HttpClient在url中使用特殊字符 - WinRT

时间:2016-05-30 15:13:51

标签: c# windows-runtime httpclient

我试图用这段代码做一个帖子

try
        {


            using (var httpClient = new HttpClient { BaseAddress = Constants.baseAddress })
            {
                var content = new StreamContent(binaries);
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", App.Current.Resources["token"] as string);
                App.Current.Resources["TaskUpload"] = true;
                using (var response = await httpClient.PostAsync("file?fileName=" + filePath, content))
                {

                    string responseData = await response.Content.ReadAsStringAsync();
                    if (responseData.Contains("errorCode"))
                        throw new Exception("Exception: " + responseData);

                    else
                    {
                        JsonObject jObj = new JsonObject();
                        JsonObject.TryParse(responseData, out jObj);
                        if (jObj.ContainsKey("fileId"))
                        {
                            if (jObj["fileId"].ValueType != JsonValueType.Null)
                            {
                                App.Current.Resources["NewVersionDoc"] = jObj["fileId"].GetString();
                            }
                        }

                    }
                    return true;
                }
            }
        }
        catch (Exception e)
        { ... }

除了在我有这个文件路径的特殊情况下,它运行良好 " [57481970e821f7f95a7b51ab] / ixair / 2015-08-29T06-22交易#796230257156844-1483918_v01_signed_2_signed_signed_signed_signed_signed_signed__v01.pdf"

如果我看到RequestMessage,我得到的回复

{Method: POST, RequestUri: 'https://xxx.xxxxx.xxx/nodeapi/v1/file?fileName=[57481970e821f7f95a7b51ab]/ixair/2015-08-29T06-22 Transaction
#796230257156844-1483918_v01_signed_2_signed_signed_signed_signed_signed_signed__v01.pdf', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: {   Authorization: Bearer 1iIvQBegqtaZgvPmDk1GGHpFOBR3M8C0nVsxxxxxxx=   Content-Length: 240578 }}

但是在Fiddler上,我看到了这个请求 https://xxx.xxxx.xxx/nodeapi/v1/file?fileName=[57481970e821f7f95a7b51ab]/ixair/2015-08-29T06-22%20Transaction%20

我应该如何正确地考虑我在filePath上的非常长而奇怪的字符串?

1 个答案:

答案 0 :(得分:3)

尝试对Uri.EscapeDataString

使用filePath方法

<强>更新

使用FileName代替FilePath