This SOAP is making my eyes sting! I'm trying to send a command to a television. I saw the results of a packet sniff online and I am trying to duplicate it. I haven't had a chance to use a sniffer on my code but I have seen in other posts that sometimes the HttpRequest changes headers and such and that I may need to use sockets instead. Does anyone have any ideas or sample code on how to achieve this?
I have some variables that I use to set custom headers (Common.ForPOSTSetHeaderKey1, etc). In my code sample below I am only setting the User-Agent and SOAPACTION headers but I have also tried setting the HOST and Content-Type.
Originally I hadn't rewritten the user-agent and I was getting "connection refused by the host". After adding the user-agent part it I no longer got that error but rather a 400 error (bad request). I'm not sure what else to do at this point as it all looks right to me.
Packet sniff:
My code:
I have some variables that I use to set custom headers (Common.ForPOSTSetHeaderKey1, etc). In my code sample below I am only setting the User-Agent and SOAPACTION headers but I have also tried setting the HOST and Content-Type.
Originally I hadn't rewritten the user-agent and I was getting "connection refused by the host". After adding the user-agent part it I no longer got that error but rather a 400 error (bad request). I'm not sure what else to do at this point as it all looks right to me.
Packet sniff:
POST /nrc/control_0 HTTP/1.1
User-Agent: Panasonic iOS VR-CP UPnP/2.0
Host: 10.0.1.155:55000
Content-Type: text/xml; charset="utf-8"
SOAPACTION: "urnanasonic-com:service
00NetworkControl:1#X_SendKey"
Content-Length: 331
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:X_SendKey xmlns:u="urnanasonic-com:service
00NetworkControl:1">
<X_KeyEvent>NRC_MUTE-ONOFF</X_KeyEvent>
</u:X_SendKey>
</s:Body>
</s:Envelope>
My code:
B4X:
Dim QT As String = Chr(34)
' Build SOAP command and send POST
Common.ForPOSTSetHeaderKey1 = "User-Agent"
Common.ForPOSTSetHeaderVal1 = "Panasonic iOS VR-CP UPnP/2.0"
Common.ForPOSTSetHeaderKey2 = ""
Common.ForPOSTSetHeaderVal2 = ""
Common.ForPOSTSetHeaderKey3 = "SOAPACTION"
Common.ForPOSTSetHeaderVal3 = "urn:panasonic-com:service:p00NetworkControl:1#X_SendKey"
Dim DestURL As String
Dim SoapRequest As String
DestURL = "http://" & Common.AVRIP & ":" & Common.AVRPort & "/nrc/control_0"
SoapRequest = "<?xml version='1.0' encoding='utf-8'?>" & _
"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'>" & _
"<s:Body>" & _
"<u:X_SendKey xmlns:u='urn:panasonic-com:service:p00NetworkControl:1'>" & _
"<X_KeyEvent>$COMMAND$</X_KeyEvent>" & _
"</u:X_SendKey>" & _
"</s:Body>" & _
"</s:Envelope>"
SoapRequest = SoapRequest.Replace ("'", QT) ' Replace ' with real quotes
SoapRequest = SoapRequest.Replace ("$COMMAND$", sCMD) ' Replace $COMMAND$ with the desired command
SendPOST (DestURL,SoapRequest)
' Dim Job1 As HttpJob
' Job1.Initialize ("Job1", Me)
' Job1.PostString (DestURL,SoapRequest)
End Sub
Sub SendPOST (URL As String, PostData As String) ' Used for HTTP execution
If Common.hcIsInitialized = False Then
Common.hc.InitializeAcceptAll("hc")
Common.hcIsInitialized = True
End If
Log (URL)
Log (PostData)
PostBytes(URL, PostData.GetBytes("UTF8"))
End Sub
'Sends a POST request with the given string as the post data
Public Sub PostBytes(Link As String, Data() As Byte)
Dim req As HttpRequest, taskID As Int
req.InitializePost2(Link, Data)
If Common.ForPOSTSetHeaderKey1 <> "" Then
req.SetHeader (Common.ForPOSTSetHeaderKey1, Common.ForPOSTSetHeaderVal1)
End If
If Common.ForPOSTSetHeaderKey2 <> "" Then
req.SetHeader (Common.ForPOSTSetHeaderKey2, Common.ForPOSTSetHeaderVal2)
End If
If Common.ForPOSTSetHeaderKey3 <> "" Then
req.SetHeader (Common.ForPOSTSetHeaderKey3, Common.ForPOSTSetHeaderVal3)
End If
req.Timeout = 45000
If taskID = 0 Then taskID = 50
taskID = taskID + 1
If taskID = 89 Then taskID = 50
Common.hc.Execute(req, taskID)
End Sub