HttpWebRequest does not handle redirect!!

J

Jon Davis

I have an HttpWebRequest object that is performing an HTTP POST query on an
HTTP server that is returning the proper status code of either 301 or 303
(redirect) and a Location header, but the HttpWebRequest object isn't
following it and is returning the full body of the initial HTTP response
instead. AllowAutoRedirect defaults to True, but setting it to True doesn't
help.

What's the deal?

Jon


// XML-RPC Client
public object Call(Uri uri, string Method, params object[] Params) {
// build and send request
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(uri);
req.Proxy = ProxySettings;
req.CookieContainer = Cookies;
for (int i=0;i<Certificates.Count;i++) {
req.ClientCertificates.Add(Certificates);
}
req.ContentType = "text/xml";
req.Method = "POST";
req.UserAgent = UserAgent;
for (int i=0;i<Headers.Count;i++) {
req.Headers.Add(Headers);
}
string reqBody = BuildRequest(Method, Params, Encoding);
System.Text.UTF8Encoding encoding = new UTF8Encoding();
byte[] byteReqBody=encoding.GetBytes(reqBody);
req.ContentLength = byteReqBody.Length;
Stream st;
WebResponse wr;
StreamReader sr;
try {
st = req.GetRequestStream();
} catch (Exception ex) {
throw new XmlRpcConnectException(ex.Message, ex);
}
st.Write(byteReqBody, 0, byteReqBody.Length);
st.Flush();
// receive and parse response
wr = req.GetResponse();
sr = new StreamReader(wr.GetResponseStream());
string resp = sr.ReadToEnd();
try { sr.Close();} catch {}
try {wr.Close();} catch {}
try {st.Close();} catch {}
return XmlRpcTools.ConvertElement(ParseResponse(resp), false);

}
 
P

Peter Bromberg [C# MVP]

Try looking at:
MaximumAutomaticRedirections method property --sets the maximum number of
redirections for the request to follow

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top