In HttpClientHandler have propertie AllowAutoRedirect, but on build app for WindowsPhone throw exception:
HttpClientHandler.AllowAutoRedirect is not supported on this platform. Please check HttpClientHandler.SupportsRedirectConfiguration before using HttpClientHandler.AllowAutoRedirect.
I really want to prevent autoredirect. I tried to use HttpWebRequest:
var client = (HttpWebRequest) WebRequest.Create(connectionUrl);
client.Headers["AllowAutoRedirect"] = "false";
client.Method = "GET";
client.Headers["UserAgent"] = @"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31";
client.ContentType = "application/json";
client.Headers["ContentLength"] = client.ToString().Length.ToString();
client.BeginGetResponse(Callback, client);
private void Callback(IAsyncResult ar)
{
var requestState =(HttpWebRequest) ar.AsyncState;
using (var postStream = requestState.EndGetRequestStream(ar))
{}
}
this code throw exception on EndGetRequestStream: "Value does not fall within the expected range" I look forward to your help.
I think you are receiving the ArgumentException: Value does not fall within the expected range
because you are initiating a BeginGetResponse()
on the client but then doing a EndGetRequestStream()
in your callback where instead you should call EndGetResponse()
. Setting AllowAutoRedirect
works fine you just need to fix your code. Try this:
var client = (HttpWebRequest)WebRequest.Create(connectionUrl);
client.AllowAutoRedirect = false;
client.Method = "GET";
client.BeginGetResponse(Callback, client);
private void Callback(IAsyncResult ar) {
var state = (HttpWebRequest)ar.AsyncState;
using (var response = state.EndGetResponse(ar)) {
var streamResponse = response.GetResponseStream();
var streamRead = new StreamReader(streamResponse);
var responseString = streamRead.ReadToEnd();
}
}