V
V
Vi xsss12018-07-03 12:47:53
.NET
Vi xsss1, 2018-07-03 12:47:53

How to disable encode in WebClient UploadValues?

using (var wb = new WebClient())
{

  wb.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
  var response = wb.UploadValues(url, "POST", data);
  responseInString = Encoding.UTF8.GetString(response);
}

For example request: http://site.com/index.php?199|¶m=1
The request is sent to site.com/index.php?199%7C¶m=1
But the foreign server does not understand 199%7C only 199|
How to send a request with the symbol | through WebClient.
When I make a curl request to php, if I need to encode, I do it manually. How can I disable encode here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nemiro, 2018-07-04
@xsss1

If the parameter is in Url , then you can instantiate the Uri and disable encoding:

var url = new Uri("http://site.com/index.php?199|&param=1", true);
// ...
var response = wb.UploadValues(url, "POST", data);

Without parameter encoding with HttpClient
using (var client = new HttpClient())
{
  var url = new Uri("http://site.com/index.php?199|&param=1", true);
  var data = new StringBuilder();
  data.AppendLine("abc=199|");

  using (var content = new StringContent(data.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded"))
  {
    using (var request = new HttpRequestMessage(HttpMethod.Post, url))
    {
      request.Content = content;

      using (var response = client.SendAsync(request).Result)
      {
        response.EnsureSuccessStatusCode();

        // var result = await response.Content.ReadAsStringAsync();
        var result = response.Content.ReadAsStringAsync().Result;
      }
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question