• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

error Posting XML with characters in a Mac Address

I am posting an XML string to a server that logs users connecting to my mySQL database.
one of the variables is a Mac Addresseg: fe80::25ea:dfaa:bdd0:32f%11.

Im guessing the issue is the non alphanumeric characters as non utf-8 ?  but im not sure
here is the code Im using maybe someone can help me figure out why my program is crashing because of the IP address.

Imports System
Imports System.IO
Imports System.Net
Imports System.Text
Public Class logVbUser
    Public Shared responseFromVbLogger As String
    Public Shared Sub Main(ByVal customerId As Integer, ByVal userId As Integer, ByVal pcName As String, ByVal macAddress As String)
        Dim xml As String
        xml = "logRequestMode=X&logRequestData="
        xml += "<?xml version='1.0' encoding='UTF-8'?>"
        xml += "<TranxRequest>"
        xml += "<CompanyId>" & customerId & "</CompanyId>"
        xml += "<UserId>" & userId & "</UserId>"
        xml += "<pcName>" & pcName & "</pcName>"
        xml += "<macAddress>" & macAddress & "</xxxName>"
        xml += "</TranxRequest>"

        ' Create a request using a URL that can receive a post. 
        Dim request As WebRequest = WebRequest.Create("http://" & databaseConnection.dbServer & "/includes/vbNetLogger.php")
        ' Set the Method property of the request to POST.
        request.Method = "POST"
        ' Create POST data and convert it to a byte array.
        Dim postData As String = xml
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        ' Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded"
        ' Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length
        ' Get the request stream.
        Dim dataStream As Stream = request.GetRequestStream()
        ' Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length)
        ' Close the Stream object.
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        ' Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        ' Display the content.
        responseFromVbLogger = responseFromServer
        ' Clean up the streams.
    End Sub
End Class

Open in new window

  • 7
  • 7
5 Solutions
It's not a MAC address, it's an IPv6 address...

You should pass a correct ContentType value, like
request.ContentType = "text/xml; charset=UTF-8"

Open in new window

Do you know where exactly your program is crashed, at what line? Is there any exception, error code or something?
prowebinteractiveincAuthor Commented:
the .NET program is not crashing its the web server that is trying to read and parse the XML string because of invalid characters. is there a way to get the Mac address instead of the IPv6
I want to be able to pin point the computer used to login to the system. any suggestions ?
prowebinteractiveincAuthor Commented:
if I take out line 15 of my code everything works great...
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Do you mean the MAC address of the machine where the code is currently executed?
Try GetPhysicalAddress
   For Each nic In NetworkInterface.GetAllNetworkInterfaces()
      nic.GetPhysicalAddress()    ' physical (MAC) address
   next nic

Open in new window

Dit you try to change the line 26 to the one I suggested?
My guess - the "%11" in the line 15 is unescaped on the server to the ASCII control code DC1, which could affect the server somehow...
Because the server treat your message as URL form encoded, not as an XML.
You need to tell to the server the correct content type, so it will know what it exactly is in the HTTP body.
prowebinteractiveincAuthor Commented:
as per line 26, I did try changing it, the web server receiving the response doesnt reply anymore.

as for your GetPhysicalAddress... any imports ?
Imports System.Net.NetworkInformation
prowebinteractiveincAuthor Commented:

ok I have 2 network adapters on my computer, is there a way to display only the one that is being used to connect ?
To check the adapter status...
if nic.OperationalStatus = OperationalStatus.Up then 'get the address

Open in new window

Does that code execute on your application's client's PC? In other words, do you need to know just MAC address of the PC where the code is executed? Not some other network node?
prowebinteractiveincAuthor Commented:
the code does execute on the clients PC.. I am trying to gather the most information about the users PC for security purposes...

last question,  what type is nic.GetPhysicalAddress()
can it be converted as a String ?
prowebinteractiveincAuthor Commented:
can I get system OS ?
Yes, it should be convertable to string

Open in new window

To get the OS version, try this:
Imports System

str = Environment.OSVersion.VersionString   '  OS version string

Open in new window

prowebinteractiveincAuthor Commented:
the code Im using that you gave me is looping through 6x, first time is empty the rest give all the same physical address?
Well, you could try to filter out by the interface type which is the value of the NetworkInterfaceType property
if nic.NetworkInterfaceType = NetworkInterfaceType.Ethernet then ' use this adapter

Open in new window

BTW, if you execute in the command prompt this command: "ipconfig /all", will you see all 6 entries?

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now