473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

post large xml files

I have a routine I call to post XML from vb.net to a url over http

It can be the XML file can be quite a size sometime I expect it to
exceed 10mb.
I started using a routine to post the file to a URL from vb.net, but
didnt realise there would be a restiction on post size.
So from this function :

Dim result As String = ""
Dim mywriter As IO.StreamWriter
Dim strXML = xmlDoc.InnerXml()
Dim bojRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(postUrl)

bojRequest.Method = "POST"
bojRequest.ContentLength = strXML.Length
bojRequest.ContentType = "text/xml"

Dim myCred As New System.Net.NetworkCredential(postUsername,
postPassword)
bojRequest.Credentials = myCred

Try
mywriter = New
IO.StreamWriter(bojRequest.GetRequestStream())
mywriter.Write(strXML)
mywriter.Flush()
mywriter.Close()
mywriter = Nothing

Dim objResponse As System.Net.HttpWebResponse =
bojRequest.GetResponse()
Dim sr As IO.StreamReader
sr = New IO.StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd
sr.Close()
sr = Nothing
Catch ex As Exception
Return ex.Message
End Try

I got this return:
Bytes to be written to the stream exceed the Content-Length bytes size
specified, but the function works when sizes are good.
Im not sure how to fix this so I googled areound and came up with this:

Try
Dim req As WebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
Dim payload As String
req = WebRequest.Create(postUrl)
req.Method = "POST"
req.ContentType = "text/xml"
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
Dim result As WebResponse

payload = xmlDoc.InnerXml
If payload <Nothing Then
Dim i As Integer = 0
Dim j As Integer
While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i,
payload.Length - i)))

'UrlEncoded.Append(URLEncode(payload.Substring(i, payload.Length - i)))
Exit While
End If

UrlEncoded.Append(HttpUtility.UrlEncode(payload.Su bstring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes =
System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToSt ring())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
Else
req.ContentLength = 0
End If
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)

Console.WriteLine()
Console.WriteLine("Response stream received")
Dim read(256) As Char
Dim count As Integer = sr.Read(read, 0, 256)

Console.WriteLine("HTML...")
Console.WriteLine()
Do While count 0
Dim str As String = New String(read, 0, count)
Console.Write(str)
count = sr.Read(read, 0, 256)
Loop
Console.WriteLine("")
Catch Exc As Exception
Console.WriteLine()
Console.WriteLine("The request URI could not be found or
was malformed")
Finally
If Not result Is Nothing Then
result.Close()
End If
End Try

The function runs through without error, but doesnt post
anything..im not sure why its not posting, the url and credentials are
fine.
So I end up with 2 questions, one how to run a procedure to
post large xml files over http, and if anyone knows how to use
compression to reduce the file size sent?

Oct 1 '06 #1
2 5878


andy wrote:

Dim strXML = xmlDoc.InnerXml()
What is xmlDoc? A System.Xml.XmlDocument instance? If so if you want to
write that to a stream then you can simply do e.g.
Dim bojRequest As System.Net.HttpWebRequest =
System.Net.HttpWebRequest.Create(postUrl)

bojRequest.Method = "POST"
bojRequest.ContentType = "text/xml"

Dim myCred As New System.Net.NetworkCredential(postUsername,
postPassword)
bojRequest.Credentials = myCred
Stream requestStream = bojRequest.GetRequestStream()
xmlDoc.Save(requestStream)

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 1 '06 #2
Thanks for replying Martin.
I could see what you were saying but couldnt get it to work, anyway
here is the solution i found earlier for anyone else trawling google.

Dim inputStream As New FileStream(filePath, FileMode.Open,
FileAccess.Read, FileShare.Read)
Dim inputLength As Integer = CInt(inputStream.Length)
Dim inputBody(inputLength) As Byte
inputStream.Read(inputBody, 0, inputLength)
inputStream.Close()
' Create WebRequest and set headers
Dim req As HttpWebRequest =
CType(WebRequest.Create(postUrl), HttpWebRequest)
req.Credentials = CredentialCache.DefaultCredentials
req.Method = "POST"
req.ProtocolVersion = HttpVersion.Version11
req.ContentType = "text/xml"
req.ContentLength = inputLength
If 0 < mTimeout Then
req.Timeout = mTimeout
End If
' Write the input to the request stream
Dim reqStream As Stream = req.GetRequestStream()
reqStream.Write(inputBody, 0, inputLength)
reqStream.Close()
It works althought im not at all sure about this functions limitations
when faced with large file sizes and would still be interested in
comments on passing xml over http with compression.
Seeing as compression reduces significantly the data been sent over the

wire and can go someway to securing raw data....
Anyone comments ?

Oct 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Buddy Ackerman | last post by:
I'm trying to write files directly to the client so that it forces the client to open the Save As dialog box rather than display the file. On some occasions the files are very large (100MB+). On...
2
by: jdev8080 | last post by:
We are looking at creating large XML files containing binary data (encoded as base64) and passing them to transformers that will parse and transform the data into different formats. Basically,...
16
by: lawrence k | last post by:
I've a file upload script on my site. I just now used it to upload a small text document (10k). Everything worked fine. Then I tried to upload a 5.3 meg Quicktime video. Didn't work. I've...
14
by: Robert S | last post by:
I am trying to use POST to transfer data to another page. When I do this, '.' characters get converted to"_". For example: #index.html: <form action="test.php" method="post"> <input...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
6
by: Brybot | last post by:
I am trying to allow HTTP POST file uploads to my web service. Currently I have it working perfectly for a SOAP/XML request reading in a byte using MemoryStream/FileStream but I cannot figure out...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
4
by: Wolfgang Draxinger | last post by:
I'm thinking about writing a script to upload videos to sites like YouTube or Google Video, which is usually done by a HTTP POST. The problem is, that videos, by nature are rather big files,...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.