HttpWebRequest/XmlWriter questions

J

jrad

Hello,

I have an app that is trying to send an xml document to a server that
contains a large piece of data (ie, mp3) embedded inside.
I am using the HttpWebRequest and XmlWriter classes to "stream" the
xml file to the server, because
the entire xml file will be too large to represent in memory if the
mp3 file is big (30 minutes long).

I have created the following method to create the xml streamer:

public static bool OpenRequestStream(string url)
{
// req is a class member
req = (HttpWebRequest)WebRequest.Create(new Uri(url)));

req.Method = "POST";
req.ContentType = @"text/xml; charset=""utf-8""";//@"text/xml";
req.Timeout = 120000; //What would be best Timeout value?
req.AllowWriteStreamBuffering = true;
req.Pipelined = true;
req.KeepAlive = true;

ServicePointManager.CertificatePolicy = new MyPolicy();

// ContentLength is not set becaue I do not know the length
// of the entire xml file I am sending
//req.ContentLength = encodedBytes.Length;
req.SendChunked = true;
requestStream = req.GetRequestStream();

// xmlWriterSettings is a class member
xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;
xmlWriterSettings.OmitXmlDeclaration = true;
xmlWriterSettings.Indent = false;

// myXmlWriter is a class member
myXmlWriter = XmlWriter.Create(requestStream, xmlWriterSettings);
}


I have also created methods to write xml data to the stream.
This should send the xml to the server before returning, correct?

public static void insertXmlToStream(byte[] xml)
{
myXmlWriter.WriteBase64(xml, 0, xml.Length); ;
myXmlWriter.Flush();
}

public static void insertXmlToStream(string xml)
{
myXmlWriter.WriteRaw(xml);
myXmlWriter.Flush();
}


I then created a method to close the connection and get the response
from the server.

public static string CloseRequestStream()
{
string msg = "";
string responseFromServer = "";

myXmlWriter.Close();
myXmlWriter = null;

requestStream.Close();
requestStream = null;

response = (HttpWebResponse)req.GetResponse();
st = response.GetResponseStream();
myReader = new StreamReader(st);
responseFromServer = myReader.ReadToEnd();

}

When I get to the myXmlWrite.Close() call, an exception is thrown that
indicates:

Specified argument was out of the range of valid values.
Parameter name: size

Stack Trace:
at System.Net.HttpWriteStream.Write()
at System.Xml.XmlUtf8RawTextWriter.FlushBuffer()
at System.Xml.XmlUtf8RawTextWriter.Close()
at System.Xml.XmlRawWriter.Close()
at System.Xml.XmlWellFormedWriter.Close()
at RDMS.Connection.CloseRequestStream()

Question:
1. I not new to .NET CF, but am new to the HttpWebRequest and
XmlWriter classes, should my code "stream" the xml to the server?

2. What do I need to modify so that I do not get an exception?

Thanks,
JR
 

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