|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Invalid Characters, Posting XML Data, WebrequestI 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 xmlns="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.CountryDataTable, ByVal emailTable As TachonetPing_Con.TachonetPingDataSet.EmailDataTable, ByVal NetworkTimeoutMilliseconds As Integer) _emailTable = emailTable Dim cert As X509Certificate = X509Certificate.CreateFromCertFile(My.Settings.ClientCertificatePath) 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.EmailDataTable) 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.ServerCertificateValidationCallback = 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.RemoteCertificateChainErrors 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 Thus wrote romiko2***@yahoo.co.uk,
> Hi Folks, These are not "invalid characters". It's the UTF-8 byte order mark (BOM).> > 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? 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 news-re***@joergjooss.de
Other interesting topics
ASP NET server can not response after long job
A serious bug?? Building other sln (to build references) User control accessing outside control Textbox in User Control error -does not exist in current context determine sql decimal precision in .net Reading files in a pages directory SqlDataSource Datasource doesn't generate data GridView: Calculated Field |
|||||||||||||||||||||||