473,287 Members | 3,295 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,287 software developers and data experts.

Invalid Characters, Posting XML Data, Webrequest

Hi Folks,

I got a weird problem, I create an XMLWriter to post a document via the
webrequest stream and after running a network trace, I notice the data
is prefixed with 3 invalid characters!

The invalid characters are: ef bb bf, at the top of the stream, which
are . Why is this occuring, I am totally baffled by this?

Here is the stream:
000000A5 ef bb bf 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e ...<?xml
version
000000B5 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d ="1.0" e
ncoding=
000000C5 22 75 74 66 2d 38 22 3f 3e 3c 54 43 4e 5f 52 65 "utf-8"?
<TCN_Re

000000D5 63 65 69 70 74 20 78 6d 6c 6e 73 3d 22 75 72 6e ceipt xm
lns="urn
000000E5 3a 65 75 2e 63 65 63 2e 74 72 65 6e 2e 74 63 6e :eu.cec.
tren.tcn
000000F5 22 3e 3c 48 65 61 64 65 72 20 56 65 72 73 69 6f "><Heade r
Versio
00000105 6e 3d 22 31 2e 34 22 20 54 65 73 74 49 64 3d 22 n="1.4"
TestId="
00000115 54 65 73 74 49 64 5f 31 22 20 4d 53 52 65 66 49 TestId_1 "
MSRefI
00000125 64 3d 22 4d 53 52 65 66 49 64 5f 31 22 20 54 43 d="MSRef
Id_1" TC
00000135 4e 52 65 66 49 64 3d 22 30 30 30 30 30 30 30 30 NRefId="
00000000
00000145 2d 30 30 30 30 2d 30 30 30 30 2d 30 30 30 30 2d -0000-00
00-0000-
00000155 30 30 30 30 30 30 30 30 30 30 30 30 22 20 53 65 00000000
0000" Se
00000165 6e 74 41 74 3d 22 31 39 39 39 2d 30 39 2d 30 39 ntAt="19
99-09-09
00000175 54 30 30 3a 30 30 3a 30 30 2d 30 30 3a 30 30 22 T00:00:0
0-00:00"
00000185 20 46 72 6f 6d 3d 22 55 6e 6b 6e 6f 77 6e 22 20 From="U
nknown"
00000195 54 6f 3d 22 55 6e 6b 6e 6f 77 6e 22 20 53 74 61 To="Unkn
own" Sta
000001A5 74 75 73 43 6f 64 65 3d 22 4f 4b 22 20 53 74 61 tusCode=
"OK" Sta
000001B5 74 75 73 4d 65 73 73 61 67 65 3d 22 54 68 69 73 tusMessa
ge="This
000001C5 20 69 73 20 61 20 6d 65 73 73 61 67 65 20 73 65 is a me
ssage se
000001D5 6e 74 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 nt autom
atically
000001E5 20 62 79 20 74 68 65 20 54 43 4e 20 70 72 6f 62 by the TCN
prob
000001F5 65 20 74 6f 6f 6c 2c 20 70 6c 65 61 73 65 20 69 e tool,
please i
00000205 67 6e 6f 72 65 20 69 74 2c 20 74 68 61 6e 6b 73 gnore it ,
thanks
00000215 22 3e 3c 2f 48 65 61 64 65 72 3e 3c 2f 54 43 4e "></Head
er></TCN
00000225 5f 52 65 63 65 69 70 74 3e _Receipt >

Here is the code:
Imports System.Security.Cryptography.X509Certificates
Imports System.Net
Imports System.Net.Security
Imports System.Xml
Imports System.Security.Policy 'Important for SSL validation checks
Public Class HTTPPost

Dim cName As String
Dim wReq As HttpWebRequest
Dim wResp As WebResponse
Dim xmlWriter As XmlTextWriter
Dim timeNow As DateTime
Dim interval As Double
Dim xmlDoc As New XmlDocument
Dim httpCode As String
Dim statusMessage As String
Dim secondsToComplete As String
Dim DataWriter As New DataWriter
Dim c_url As String
Dim email As New Email
Dim _emailTable As TachonetPingDataSet.EmailDataTable
Public Sub GenerateXML(ByVal countryTable As
TachonetPing_Con.TachonetPingDataSet.CountryDataTa ble, ByVal emailTable
As TachonetPing_Con.TachonetPingDataSet.EmailDataTabl e, ByVal
NetworkTimeoutMilliseconds As Integer)
_emailTable = emailTable
Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile(My.Settings.Cli entCertificatePath)
For Each dr As TachonetPing_Con.TachonetPingDataSet.CountryRow
In countryTable
If Not dr(2) Is System.DBNull.Value Then
Dim xmlString As String = TCNReceipt.CreateXML("TCN_" &
dr(0))
SendXML(dr(1), dr(2), xmlString, cert,
NetworkTimeoutMilliseconds, emailTable)
End If

Next
End Sub

Private Function SendXML(ByVal countryName As String, ByVal url As
String, ByVal xmlText As String, ByVal cert As X509Certificate, ByVal
NetworkTimeoutMilliseconds As Integer, ByVal emailTable As
TachonetPing_Con.TachonetPingDataSet.EmailDataTabl e) As Boolean
Try
cName = countryName
c_url = url
xmlDoc.LoadXml(xmlText)
'Setup the delegate with the address of the handling
function for certificate validation.
ServicePointManager.ServerCertificateValidationCal lback =
New RemoteCertificateValidationCallback(AddressOf MyCertValidationCb)
timeNow = Date.Now 'Get the time which will be used by
exceptions
wReq = HttpWebRequest.Create(url) ' Connect to URL
wReq.ClientCertificates.Add(cert)
wReq.Timeout = NetworkTimeoutMilliseconds

wReq.Method = "POST" 'Set HTTP Method
wReq.ContentType = "text/xml charset=utf-8" ' Set Content
Type
'Open Stream
xmlWriter = New XmlTextWriter(wReq.GetRequestStream(),
System.Text.Encoding.UTF8)
'Send the XML Data
xmlDoc.WriteTo(xmlWriter)
xmlWriter.Close()

'Get Response Stream
timeNow = Date.Now 'If an exception did not occur, grab the
time here
wResp = wReq.GetResponse
If TypeOf wResp Is HttpWebResponse Then
httpCode = CType(wResp, HttpWebResponse).StatusCode
'Convert to HTTPWebResponse
If httpCode = HttpStatusCode.Accepted Or httpCode =
HttpStatusCode.OK Then
interval = Date.Now.Subtract(timeNow).TotalSeconds
secondsToComplete = interval.ToString("f3")
statusMessage = "UP - Response Time: " &
secondsToComplete & " milliseconds"
DataWriter.UpdateAlertTracker(Date.Now,
countryName, statusMessage)
Else
statusMessage = "Down - HTTP Code: " & httpCode
DataWriter.UpdateAlertTracker(Date.Now,
countryName, statusMessage)
WindowsEventLogger.LogMessageWarning(countryName &
": " & statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail(cName, httpCode, emailTable)
End If
End If
wResp.Close()

Catch wex As WebException
If Not CType(wex.Response, Net.HttpWebResponse) Is Nothing
Then
httpCode = CType(wex.Response,
Net.HttpWebResponse).StatusCode
statusMessage = "Down - HTTP Code: " & httpCode
DataWriter.UpdateAlertTracker(Date.Now, countryName,
statusMessage)
WindowsEventLogger.LogMessageWarning(countryName & ": "
& statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail(cName, "httpCode: " & httpCode,
emailTable)
Else
httpCode = "n/a"
statusMessage = wex.Message
statusMessage = "Down: " & wex.Message
DataWriter.UpdateAlertTracker(Date.Now, countryName,
statusMessage)
WindowsEventLogger.LogMessageWarning(countryName & ": "
& statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail(cName, "httpCode: " & httpCode,
emailTable)
End If

Catch ex As Exception
statusMessage = ex.Message
httpCode = "n/a"
DataWriter.UpdateAlertTracker(Date.Now, countryName,
statusMessage)
WindowsEventLogger.LogMessageCritical(countryName & ": " &
statusMessage & vbNewLine & "HTTPCode: " & httpCode)
Throw
Finally
If Not xmlWriter Is Nothing Then
xmlWriter.Close()
End If
If Not wResp Is Nothing Then
wResp.Close()
End If
End Try
End Function

Public Function MyCertValidationCb(ByVal sender As Object, ByVal
certificate As X509Certificate, ByVal chain As X509Chain, ByVal
sslPolicyErrors As SslPolicyErrors) As Boolean

If sslPolicyErrors =
Security.SslPolicyErrors.RemoteCertificateChainErr ors Then
WindowsEventLogger.LogMessageWarning(cName & ": Remote
Certificate Chain Error" & vbNewLine & c_url)
email.SendEmail(cName, "Certificate Error: Remote
Certificate Chain Error", _emailTable)
Return False
ElseIf sslPolicyErrors =
sslPolicyErrors.RemoteCertificateNameMismatch Then
Dim z As Zone
z = Zone.CreateFromUrl(CType(sender,
HttpWebRequest).RequestUri.ToString())
If (z.SecurityZone = System.Security.SecurityZone.Intranet
Or z.SecurityZone = System.Security.SecurityZone.MyComputer) Then
Return True
Else
WindowsEventLogger.LogMessageWarning(cName & ": Remote
Certificate Name Mismatch" & vbNewLine & c_url)
email.SendEmail(cName, "Certificate Error: Remote
Certificate Name Mismatch", _emailTable)
Return False
End If
ElseIf sslPolicyErrors =
sslPolicyErrors.RemoteCertificateNotAvailable Then
WindowsEventLogger.LogMessageWarning(cName & ": Remote
Certificate not available" & vbNewLine & c_url)
email.SendEmail(cName, "Certificate Error: Remote
Certificate not available", _emailTable)
Return False
ElseIf sslPolicyErrors = sslPolicyErrors.None Then
Return True
End If
End Function
End Class

May 19 '06 #1
1 2955
Thus wrote ro********@yahoo.co.uk,
Hi Folks,

I got a weird problem, I create an XMLWriter to post a document via
the webrequest stream and after running a network trace, I notice the
data is prefixed with 3 invalid characters!

The invalid characters are: ef bb bf, at the top of the stream,
which are . Why is this occuring, I am totally baffled by this?


These are not "invalid characters". It's the UTF-8 byte order mark (BOM).

To omit it, change

xmlWriter = New XmlTextWriter(wReq.GetRequestStream(),
System.Text.Encoding.UTF8)

to

' Don't trust me writing proper VB .NET code ;-)
Dim Encoding utf8 as Encoding
utf8 = New UTF8Encoding()
xmlWriter = New XmlTextWriter(wReq.GetRequestStream(), utf8)

The default instance System.Text.Encoding.UTF8 prepends a BOM, but if you
create your own UTF8Encoding instance, you can change this behaviour.

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 20 '06 #2

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

Similar topics

3
by: Kaidi | last post by:
Hello guys, I get the "an invalid XML character" error when using xerces to parse a XML file. I know that XML will correspond the &, <, >, " to special strings like "&gt;&lt;". However, how about if the...
1
by: LD | last post by:
Hi, I'm pulling my hair out!! My problem is, I need to automatically upload a zip file along with 3 other pieces of text data to a web server and wait for it's xml response. Basically a...
3
by: Manuel | last post by:
I have an asp page ("test.asp") that presents the data it receives from a post.When I try the following code, test.asp doesn't return the values (supposedly) posted to it. If I make a web page with...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
7
by: Nadav | last post by:
Hello, When I create an XML header using this code: XmlDeclaration header = doc.CreateXmlDeclaration("1.0", "UTF-8", null); XmlElement rootElement = doc.DocumentElement; ...
2
by: adwooley2 | last post by:
Hello. Have been losing plenty of hair over problem whereby I can't make it off the login page. Trying to pass login info to a login page and then move on to another page within the site so that...
1
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I...
0
by: Solius | last post by:
I have been struggling for 4 days to write a connection to an XML REST API. I can't figure out what is wrong with my code that it won't connect propertly. The goal is to make a web service that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.