473,554 Members | 4,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.X 509Certificates
Imports System.Net
Imports System.Net.Secu rity
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 secondsToComple te As String
Dim DataWriter As New DataWriter
Dim c_url As String
Dim email As New Email
Dim _emailTable As TachonetPingDat aSet.EmailDataT able
Public Sub GenerateXML(ByV al countryTable As
TachonetPing_Co n.TachonetPingD ataSet.CountryD ataTable, ByVal emailTable
As TachonetPing_Co n.TachonetPingD ataSet.EmailDat aTable, ByVal
NetworkTimeoutM illiseconds As Integer)
_emailTable = emailTable
Dim cert As X509Certificate =
X509Certificate .CreateFromCert File(My.Setting s.ClientCertifi catePath)
For Each dr As TachonetPing_Co n.TachonetPingD ataSet.CountryR ow
In countryTable
If Not dr(2) Is System.DBNull.V alue Then
Dim xmlString As String = TCNReceipt.Crea teXML("TCN_" &
dr(0))
SendXML(dr(1), dr(2), xmlString, cert,
NetworkTimeoutM illiseconds, 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
NetworkTimeoutM illiseconds As Integer, ByVal emailTable As
TachonetPing_Co n.TachonetPingD ataSet.EmailDat aTable) As Boolean
Try
cName = countryName
c_url = url
xmlDoc.LoadXml( xmlText)
'Setup the delegate with the address of the handling
function for certificate validation.
ServicePointMan ager.ServerCert ificateValidati onCallback =
New RemoteCertifica teValidationCal lback(AddressOf MyCertValidatio nCb)
timeNow = Date.Now 'Get the time which will be used by
exceptions
wReq = HttpWebRequest. Create(url) ' Connect to URL
wReq.ClientCert ificates.Add(ce rt)
wReq.Timeout = NetworkTimeoutM illiseconds

wReq.Method = "POST" 'Set HTTP Method
wReq.ContentTyp e = "text/xml charset=utf-8" ' Set Content
Type
'Open Stream
xmlWriter = New XmlTextWriter(w Req.GetRequestS tream(),
System.Text.Enc oding.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.GetRespons e
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.Subtra ct(timeNow).Tot alSeconds
secondsToComple te = interval.ToStri ng("f3")
statusMessage = "UP - Response Time: " &
secondsToComple te & " milliseconds"
DataWriter.Upda teAlertTracker( Date.Now,
countryName, statusMessage)
Else
statusMessage = "Down - HTTP Code: " & httpCode
DataWriter.Upda teAlertTracker( Date.Now,
countryName, statusMessage)
WindowsEventLog ger.LogMessageW arning(countryN ame &
": " & statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail (cName, httpCode, emailTable)
End If
End If
wResp.Close()

Catch wex As WebException
If Not CType(wex.Respo nse, Net.HttpWebResp onse) Is Nothing
Then
httpCode = CType(wex.Respo nse,
Net.HttpWebResp onse).StatusCod e
statusMessage = "Down - HTTP Code: " & httpCode
DataWriter.Upda teAlertTracker( Date.Now, countryName,
statusMessage)
WindowsEventLog ger.LogMessageW arning(countryN ame & ": "
& statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail (cName, "httpCode: " & httpCode,
emailTable)
Else
httpCode = "n/a"
statusMessage = wex.Message
statusMessage = "Down: " & wex.Message
DataWriter.Upda teAlertTracker( Date.Now, countryName,
statusMessage)
WindowsEventLog ger.LogMessageW arning(countryN ame & ": "
& statusMessage & vbNewLine & "HTTPCode: " & httpCode)
email.SendEmail (cName, "httpCode: " & httpCode,
emailTable)
End If

Catch ex As Exception
statusMessage = ex.Message
httpCode = "n/a"
DataWriter.Upda teAlertTracker( Date.Now, countryName,
statusMessage)
WindowsEventLog ger.LogMessageC ritical(country Name & ": " &
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 MyCertValidatio nCb(ByVal sender As Object, ByVal
certificate As X509Certificate , ByVal chain As X509Chain, ByVal
sslPolicyErrors As SslPolicyErrors ) As Boolean

If sslPolicyErrors =
Security.SslPol icyErrors.Remot eCertificateCha inErrors Then
WindowsEventLog ger.LogMessageW arning(cName & ": Remote
Certificate Chain Error" & vbNewLine & c_url)
email.SendEmail (cName, "Certificat e Error: Remote
Certificate Chain Error", _emailTable)
Return False
ElseIf sslPolicyErrors =
sslPolicyErrors .RemoteCertific ateNameMismatch Then
Dim z As Zone
z = Zone.CreateFrom Url(CType(sende r,
HttpWebRequest) .RequestUri.ToS tring())
If (z.SecurityZone = System.Security .SecurityZone.I ntranet
Or z.SecurityZone = System.Security .SecurityZone.M yComputer) Then
Return True
Else
WindowsEventLog ger.LogMessageW arning(cName & ": Remote
Certificate Name Mismatch" & vbNewLine & c_url)
email.SendEmail (cName, "Certificat e Error: Remote
Certificate Name Mismatch", _emailTable)
Return False
End If
ElseIf sslPolicyErrors =
sslPolicyErrors .RemoteCertific ateNotAvailable Then
WindowsEventLog ger.LogMessageW arning(cName & ": Remote
Certificate not available" & vbNewLine & c_url)
email.SendEmail (cName, "Certificat e 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 2969
Thus wrote ro********@yaho o.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(w Req.GetRequestS tream(),
System.Text.Enc oding.UTF8)

to

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

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

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

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

Similar topics

3
19733
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 XML file really needs to contain some text like: "&#x3;&#x4;&#x14;&#x8;&#x8;"? (as content of a tag) The story is: I am writing a program to...
1
4808
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 multipart/form-data post. I have tried it using a regular html form and it works but that is no good because it leaves you at the other server where
3
7564
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 a form and the values, test.asp reports them fine. ---------------------------- Dim thePost As String =...
7
17273
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 to get it to work in Visual Basic. I tried coding it once myself from scratch and then modified a class that I found on a newsgroup (referenced...
9
6622
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 length is 642 bytes ( I have appended it to the end of this message). I suspect that the reason may have something to do with an incorrect declaration...
7
7160
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; doc.InsertBefore(header, rootElement);
2
5559
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 I can download some data, but I keep getting the login page. Any ideas on this? Here's the code:...
1
4405
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 would give it a try. First of all, I am using Visual Studio 2005 to write my program. I am using C# .NET as the language. I am running Windows XP...
0
1514
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 connects directly to the REST API. When I code it as a front end doing a form post it works fine, but I can't code the VB on the backend to do the same...
0
7580
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7781
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7538
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5421
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3543
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3531
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2003
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.