Thought
Thought

Reputation: 5856

HttpClient post with special characters in url - WinRT

I trying to do a post with this piece of code

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)
        { ... }

It works well except in a particular case where i have this has filePath "[57481970e821f7f95a7b51ab]/ixair/2015-08-29T06-22 Transaction #796230257156844-1483918_v01_signed_2_signed_signed_signed_signed_signed_signed__v01.pdf"

on the response i get if i see the RequestMessage i have

{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 }}

but on Fiddler i see this request instead https://xxx.xxxx.xxx/nodeapi/v1/file?fileName=[57481970e821f7f95a7b51ab]/ixair/2015-08-29T06-22%20Transaction%20

how should i make the request correctly considering the very long and weird string i have on filePath?

Upvotes: 1

Views: 1230

Answers (1)

Andrii Krupka
Andrii Krupka

Reputation: 4306

Try to use Uri.EscapeDataString method for your filePath value

UPDATE

Use FileName instead FilePath

Upvotes: 3

Related Questions