2

I tried to follow an answer and this is what I have now:

var response = string.Empty;

HttpContent content = new StringContent(poststring);
content.Headers.Remove("Content-Type");
content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);

//const string contentType = "multipart/form-data; boundary=<calculated when request is sent>";

HttpRequestMessage request = new HttpRequestMessage
{
     Method = HttpMethod.Post,
     RequestUri = u,
     Content = content,
};
    
var handler = new TemporaryRedirectHandler()
{
     InnerHandler = new HttpClientHandler()
     {
         AllowAutoRedirect = false
     }
};

HttpClient client = new HttpClient(handler);
HttpResponseMessage result = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
return result;

Problem is that I still get a 200 and when I look at my request in Fiddler I don't see the AllowAutoRdirect set to false anywhere. WHere should i capture that field to include it in the request?

ggeorge
  • 1,496
  • 2
  • 13
  • 19
Arwaa Rasool
  • 49
  • 10
  • At first, the example should have no redirect. Is there a way when the remote endpoint is 200 OK with your request because of "Expect100Continue = false"? It could read the content and not redirect you. Anyway, there is a point to use async HttpClient(HttpClientHandler handler) with "handler.AllowAutoRedirect = false" instead of the sync HttpWebRequest. – PIoneer_2 Jan 18 '22 at 11:43
  • As per documentation, [HttpWebRequest](https://learn.microsoft.com/en-us/dotnet/api/system.net.httpwebrequest.allowautoredirect?view=net-6.0) will automatically retry for auth headers. – Anand Sowmithiran Jan 18 '22 at 13:06
  • see this SO answer, https://stackoverflow.com/a/28671822/14973743 – Anand Sowmithiran Jan 18 '22 at 13:17
  • Updated the question with the new solution i tried – Arwaa Rasool Jan 18 '22 at 17:05
  • The "AllowAutoRedirect" property is not sent to the server. It controls what the HttpClient object does when it receives a 302 response from the server. Do you see a 302 response and then a 200 response, or just a single 200 response? If it is a single 200, then it's either a server problem or something you are sending is wrong. – David Jan 21 '22 at 20:12
  • https://stackoverflow.com/questions/14731980/using-httpclient-how-would-i-prevent-automatic-redirects-and-get-original-statu – smoore4 Jan 24 '22 at 20:29

0 Answers0