Problem with WebRequest and WebResponse

fishberg5051

Member
Joined
Jul 21, 2006
Messages
7
Programming Experience
Beginner
Hi everybody, I'm having an issue with the webrequest and webresponse classes. Allow me to explain what's going on. I'm creating an smartdevice application using vb.net on the CF. The user fills in a few textboxes clicks a button and it creates an xml file. After the xml file is created, the user then finds that file (using a listbox) and clicks a button to upload that xml (contents as a string) to a server (asp 3.0 page) to process it and do whatever with. Below is the code for each that I have so far, but the problem I'm running into is when I get to the "GetResponse" portion of the sub, it returns an "server 500 internal server error".

I'm not entirerly sure if it's this app or my asp page not handling the incoming stream correctly. Any help on this matter would be greatly appreciated. I'm somewhat new to vb.net but an experienced programmer with vb6. Thanks in advance!


vb.net code
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] httpreq [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.HttpWebRequest
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] httpres [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Net.HttpWebResponse
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strXMLDoc [/SIZE][SIZE=2][COLOR=#008000]'this is the xml document the user has selected from the listbox
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strURI [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'this is the location of the asp page that will process the xml file
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] FileName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]FileName = ListBox1.Text
FileName = FileName.TrimStart("\")
[/SIZE][SIZE=2][COLOR=#008000]'Try
[/COLOR][/SIZE][SIZE=2]strXMLDoc = FileName
 
strURI = "[URL="http://myprocess.asp"]http://myprocess.asp[/URL]"
httpreq = httpreq.Create(strURI)
[/SIZE][SIZE=2][COLOR=#008000]'using the POST method for form processing
[/COLOR][/SIZE][SIZE=2]httpreq.Method = "POST"
[/SIZE][SIZE=2][COLOR=#008000]'set the content type to process text and xml alike
[/COLOR][/SIZE][SIZE=2]httpreq.ContentType = "text/xml"
httpreq.SendChunked = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'wrap the request with a text-based writer
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamXML [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.StreamWriter(httpreq.GetRequestStream()) [/SIZE][SIZE=2][COLOR=#008000]'this won't work if there is security (https) on the site
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'write the xml into the stream
[/COLOR][/SIZE][SIZE=2]streamXML.WriteLine(GetTextFromXMLFile(strXMLDoc))
[/SIZE][SIZE=2][COLOR=#008000]'close the xml file
[/COLOR][/SIZE][SIZE=2]streamXML.Close()
[/SIZE][SIZE=2][COLOR=#008000]'Get The Response
[/COLOR][/SIZE][SIZE=2]httpres = httpreq.GetResponse
 
[/SIZE][SIZE=2][COLOR=#008000]'Catch ex As Exception
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'End Try
 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'close our response and request and set to nothing when the request is finished
 
 
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'httpreq.GetRequestStream.Close()
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'httpres.GetResponseStream.Close()
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'httpreq = Nothing
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'httpres = Nothing
[/COLOR][/SIZE]
'**** end of sub, but this sub calls this function to write the xml to the stream
 
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] GetTextFromXMLFile([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] str [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ReadXML [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.StreamReader(str)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ret [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]ret = ReadXML.ReadToEnd()
ReadXML.Close()
GetTextFromXMLFile = ret
 
 
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]

****The following is my asp code (process.asp)
VB.NET:
<%
Option Explicit
Response.CacheControl = "no-cache"
Server.ScriptTimeout = 600
'Arrays
Dim arrDataXML 'The array that contains the unsigned bytes from the binary read of the xml document being posted to this page
'Strings
Dim strDataXML 'The string that contains the xml document posted to this page after it has been converted from an array.
 
'An XML Document is being posted and recieved by this page.  This gets the total number of bytes then converts the data to a safe array.
arrDataXML = Request.BinaryRead(Request.TotalBytes) 
 
'This function converts the array, created by the binary read of the xml document posted to this page, to a string
Function fcnArrayToString (arrDataXML) 
 '------------------------------------------------------------------------------------------------
 '  Variable Declaration and description
 Dim strTemp  'The string used as temporary container as the array is being parsed
 Dim intCounter 'The integer used to parse the array
 Dim intMaxElements  'The number of elements in the array
 Dim arrCharacter() 'The array containing the characters converted from bytes in the inital array created by the binary read
 'The number of elements in the array, used to parse the array from bytes to characters
 intMaxElements = UBound(arrDataXML)+1
 'Redefines the Character array with the correct number of elements
 Redim arrCharacter(intMaxElements) 
 '------------------------------------------------------------------------------------------------
  'This goes through all the elements in the array created by the binary read that was passed into this function and converts each to a character
 For intCounter=1 To intMaxElements
  'This stores the bytes for this element of the array in a temporary variable
  strTemp = MidB(arrDataXML, intCounter, 1) 
  'This converts the temporary variable to an ascii value then to a character and stores those characters in a new array
  arrCharacter(intCounter) = Chr(AscB(strTemp))
 Next 
  'This converts the new array containing characters into a string and returns the value
 fcnArrayToString = Join(arrCharacter, "") 
End Function 
 
'If there was data posted to this page, checked by determining if the request.totalbytes is greater than 0, the safe array is then converted to a string here by sending it to a function.
If Request.TotalBytes > 0 Then
 'The data posted to this page is converted to a string
 strDataXML = fcnArrayToString(arrDataXML)
 
 'Creates the XML Object
 Set objDataXML = CreateObject("MSXML2.DOMDocument")
 opulates the XML Object with the string created from the binary array
 objDataXML.LoadXML(strDataXML)
end if 
 
 
 
%>


sorry for such a long post but I want to be as detailed as possible. Any help once again is greatly appreciated. Thanks.
 
Answer:

Hey, I figured out what was wrong a few minutes after I posted this, but I thought I would add it in in case anyone else is looking.

You must set the Request.AllowWriteStreamBuffering = true

VB.NET:
[SIZE=2]httpreq.ContentType = "text/xml"
[/SIZE][SIZE=2][COLOR=#008000]'required to write the entire string to the buffer at once
[/COLOR][/SIZE][SIZE=2]httpreq.AllowWriteStreamBuffering = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'wrap the request with a text-based writer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] streamXML [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.IO.StreamWriter(httpreq.GetRequestStream())
[/SIZE]
 
Back
Top