HttpWebRequests and Redirect

G

Gina_Marano

Hey Guys,

This is really kicking my butt.

It appears that the server is not getting my data that I am posting...

Basically I am posting a little XML and getting back a response as to
whether it was accepted or not.
I am getting:

System.Xml.XmlException
Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e) at
....

private void SubmitWebRequest(string aDomain, string aURL)
{
string PostData = "<?xml version='1.0' ?
<orderstatus>Shipped</orderstatus>";

ASCIIEncoding encoding = new ASCIIEncoding();
System.Net.CookieContainer CookieC = new
System.Net.CookieContainer();
Byte[] Data = encoding.GetBytes(PostData);

System.Net.HttpWebRequest LoginReq =

(System.Net.HttpWebRequest)System.Net.WebRequest.Create(aDomain+aURL);

LoginReq.KeepAlive = false;
LoginReq.Method = "POST";
LoginReq.AllowAutoRedirect = false;
LoginReq.ContentType = "application/x-www-form-
urlencoded";
LoginReq.ContentLength = Data.Length;
LoginReq.CookieContainer = CookieC;

System.IO.Stream SendReq = LoginReq.GetRequestStream();
SendReq.Write(Data, 0, Data.Length);
SendReq.Close();

System.Net.HttpWebResponse LoginRes =
(System.Net.HttpWebResponse)LoginReq.GetResponse();
string sRedirect = LoginRes.Headers.Get("Location");
CookieC.Add(LoginRes.Cookies);

LoginReq =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(aDomain
+sRedirect);
LoginReq.KeepAlive = false;
LoginReq.Method = "GET";
LoginReq.ContentType = "application/x-www-form-
urlencoded";
LoginReq.AllowAutoRedirect = true;
LoginReq.CookieContainer = CookieC;

LoginRes =
(System.Net.HttpWebResponse)LoginReq.GetResponse();
StreamReader sReader = new
StreamReader(LoginRes.GetResponseStream());
string HTML = sReader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(HTML);
}

Any help would be MUCH appreciated!

GINA_M
 
G

Gina_Marano

Should mention that the web app is using URL based session, so the
first request (that doesn't have a session yet) is answered with a
redirect with the session in the url.

Gina_M
 
G

Gina_Marano

Should mention that the web app is using URL based session, so the
first request (that doesn't have a session yet) is answered with a
redirect with the session in the url.

Gina_M
 
D

Diego Jancic

Hi,
If i don't remember bad, you should write a new line between the <xml>
node and the root.
Something like this:

string PostData = "<?xml version='1.0' ?>" + Environment.NewLine +
"<orderstatus>Shipped</orderstatus>";

Good luck!,
Diego
 
G

Gina_Marano

Thanks Deigo.

99.9% of the solutions out there handle the redirect incorrectly, at
least for my purposes.

Here is the solution that works great.

http://www.codeproject.com/cs/internet/httpwebrequest_response.asp

ONE NOTE: the link assumes that the redirect URL returned is the full
URL and that is not always the case.

I added:

if (!ReUri.StartsWith("http:/", true, null))
ReUri = _Domain + ReUri;

Where the domain is the base server name. You must also add check for
"/" to make sure that you format your reUri correctly.

Gina_M
 

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