Trying to convert and old Pachube feed to Xively using Visual Studio 2013. Receive a 400 error.
Here is a snippet of my code base.It still works with my old pachube feed but after changing to xlivey api no longer works.
Dim PachubeStream As String = timestamp + "," + PT1Meter + "," + PT2Meter + "," + LevelOffset + "," + CurrentLevel + "," + PT1Raw + "," + PT2Raw + "," + PT1Offset + "," + PT2Offset + "," + RateofRise + "," + PeakRateofRise
'prepare to Upload the latest changed data to pachube for on-line data storage and plotting
Dim myWebClient As New WebClient()
uploaddatapachubekey = TextBox7.Text
myWebClient.Headers.Add("X-ApiKey", uploaddatapachubekey)
' Apply ASCII Encoding to obtain the string as a byte array.
Dim byteArray As Byte() = Encoding.ASCII.GetBytes(PachubeStream)
'added 11/11/2010
'configure uploaddata pachube port url
uploaddatapachubeport = "https://api.xively.com/v2/feeds/" + pachubeport + ".csv"
'UploadData input string using Http 1.0 PUT Method if not in test mode
'added 11/11/2010
If TestMode = False Then myWebClient.UploadData(uploaddatapachubeport, "put", byteArray)
Solved my own problem. Issue was the new format of all data submission is
"Channel Name",Timestamp UTC Format,Channel Data VBCRLF
i.e.
Dim PachubeStream As String = "PT1Meter"+","+timestamp+","+PT1Meter
Related
I have been attempting a TCP client/server program and have been playing around with GetProcesses. I am able to get the full list of processes on the client side, however it only lists one process per call.
I added a log to see what was happening and I actually am getting all the data. Maybe my issue is when I split up the data on the client side..
If you are familiar with TCPClient, or just know where I am going wrong, id greatly appreciate your input as I have only been writing in VB for a few months now and I feel quite annoyed. lol
Thanks
Id post the image here but I need more reputation.. Here is the link
https://i.ibb.co/7rS8bRY/listproc-issue.png
Client code:
Dim ns As NetworkStream = clientOne.GetStream()
Dim gs(clientOne.ReceiveBufferSize) As Byte
ns.Read(gs, 0, CInt(clientOne.ReceiveBufferSize))
Dim rs As String = Encoding.ASCII.GetString(gs)
Dim id() As String = Split(rs, "*", -1, CompareMethod.Text)
Dim tri As String = id(1)
If id(0) = 14 Then
Try
Dim idsa As Integer = tri.IndexOf("#")
Dim fdsa As String = tri.Substring(idsa + 1, tri.IndexOf("&", idsa + 1) - idsa - 1)
Dim idsb As Integer = tri.IndexOf("%")
Dim fdsb As String = tri.Substring(idsb + 1, tri.IndexOf("#", idsb + 1) - idsb - 1)
Dim idsc As Integer = tri.IndexOf("!")
Dim fdsc As String = tri.Substring(idsc + 1, tri.IndexOf("$", idsc + 1) - idsc - 1)
LVProc.Items.Add(New ListViewItem({fdsa, fdsb, fdsc}))
TextBox1.Text = TextBox1.Text + vbNewLine + "Process added to list" + vbNewLine
LVProc.Refresh()
Catch
End Try
End If
If id(0) = 13 Then
TextBox1.Text = TextBox1.Text + vbNewLine + "Unable to list Process" + vbNewLine
End If
Server code:
If id(0) = 10 Then
Try
For Each findp As Process In Process.GetProcesses()
If findp.MainWindowTitle = "" Then
Else
Dim msgRespa As String = "14*" + "#" + findp.Id.ToString + "&%" + findp.ProcessName + "#!" + findp.MainWindowTitle + "$"
Dim msgSenda As [Byte]() = System.Text.Encoding.ASCII.GetBytes(msgRespa)
ns.Write(msgSenda, 0, msgSenda.Length)
End If
Next findp
Catch
Dim msgResp As String = "13*"
Dim msgSend As [Byte]() = System.Text.Encoding.ASCII.GetBytes(msgResp)
ns.Write(msgSend, 0, msgSend.Length)
End Try
End If
Basically I am using .Net VB to pass in a JSON String format into Swagger-UI for Response Body.
But the Response Body displays characters like "\"
I want to get rid of that.
my Json string looks something like this in VB
sJSON = "{""csid"":""" + customer.CSID.ToString() + """}"
The Current Result is:
{\"csid\":\"1234\"}
Desired Result is:
{"csid":"1234"}
New Edits:
The string is generated from my custom print JSON class
It's the default method that came with the Controller class I mimic from HomeControllers.
I am guessing it needs to set the "application/JSON" content type?
Public Function GetValue(ByVal id As Integer) As String
Return PrintJSON()
End Function
Function PrintJSON()
Dim sJSON As String
' Begin JSON object
sJSON = "{"
' Timestamp
sJSON = sJSON + """date"":""" + DateTime.Today().ToShortDateString + ""","
sJSON = sJSON + """time"":""" + DateTime.Today().ToShortTimeString + ""","
' Return list of parameters in a JSON object
sJSON = sJSON + ""
sJSON = sJSON + """record"":["
For Each customer In Customers
sJSON = sJSON + "{"
sJSON = sJSON + """csid"":""" + customer.CSID.ToString() + """"
sJSON = sJSON + "}"
If customer.Equals(Customers.Last) Then
sJSON = sJSON + "]"
Else
sJSON = sJSON + ","
End If
Next
sJSON = sJSON + "}"
Return sJSON
End Function
Ok, I found the solution. The GetValue method can just return a data transfer object class.
Swagger will automatically display the class in JSON format.
I sure hope this was explained clearer in the Swagger tutorial.
I have created an asp.net webpage and from the webpage I am using the code below to establish a remote desktop connection.
rdp.exe is a external file
Here is my code
Protected Sub BtnRemote_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnRemote.Click
Dim Process As New System.Diagnostics.Process
Dim startinfo As New System.Diagnostics.ProcessStartInfo
startinfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
startinfo.FileName = "C:\WINDOWS\system32\cmd.exe"
Dim path = "D:\rdp.exe"
startinfo.Arguments = path + "/v:" + txtTerminal.Text + " " + "/u:" + txtTerUser.Text + " " + "/p:" + txtTerPassword.Text
startinfo.UseShellExecute = False
Process.Start(startinfo)
End Sub
Everything is fine when I debug, but the system will not establish a remote connection.
But if I use the following command from command prompt, I am able to establish a remote connection.
E:\rdp /v:"IPAddress" /domain:"domain" /u:"username" /p:"password"
A problem i can see with this is within this line (Also Notice its got the Path, User, Password, and IP, BUT is missing the Domain Argument):
startinfo.Arguments = path + "/v:" + txtTerminal.Text + " " + "/u:" + txtTerUser.Text + " " + "/p:" + txtTerPassword.Text
as stated in your CMD arguments it needs to fit this format:
E:\rdp /v:"IPAddress" /domain:"domain" /u:"username" /p:"password"
However if you notice your startinfo.arguments doesn't contain a space in between the Directory and the IP Address Line so instead of reading it like this:
E:\rdp /v:"IPAddress" /u:"username" /p:"password"
it's reading it like this:
E:\rdp/v:"IPAddress" /u:"username" /p:"password"
just simply fix the line like this:
startinfo.Arguments = path + " /v:" + txtTerminal.Text + " /u:""" + txtTerUser.Text + """ /p:" + txtTerPassword.Text
I Also removed the Concatenations of + " " + because all you really need is a
space before the /'s and it will still work properly. Also notice that i have added extra quotes, because if the username supports spaces then when it goes to set the argument if the username contains a space then it will count them as two separate arguments quotes around it prevents it from being split into two different arguments
also instead of using CMD why not start the exe directly by
Dim p As Process
p.startinfo.FileName = "D:\rdp.exe"
p.StartInfo.WorkingDirectory = "D:\"
startinfo.Arguments = path + " /v:" + txtTerminal.Text + " /u:""" + txtTerUser.Text + """ /p:" + txtTerPassword.Text
p.Start()
I have the following code , It's for countdown timer
Dim countdown As Integer = SharpTextBox6.Text
Label1.Text = "Sending to " + "" + SharpTextBox4.Text + "" + "For" + countdown.ToString - 1
the problem is it says " Conversation from string "The value ..." to double isn't valid . Why does it covert into to double ? and how I can solve this problem
You can try this :
Label1.Text = "Sending to " + SharpTextBox4.Text + "For" + (countdown - 1).ToString
MSDN: When you use the + operator, you may not be able to determine whether addition or string concatenation will occur. Use the & operator for concatenation to eliminate ambiguity and provide self-documenting code.
Label1.Text = "Sending to " & SharpTextBox4.Text & " For " & (countdown - 1)
I am currently developing an application using VB.NET in which I am using the REST WebServices. I have been able to do the basics with REST, however, I have not been able to add an attachment (more specifically upload a file, using REST which gets attached). I have done extensive research online, but so far I have not been able to find any working examples in VB.NET. To actually upload the data I use System.Net.WebClient. The following VB.NET code does the important work:
Dim Client As New System.Net.WebClient
Dim postBytes As Byte() = System.Text.Encoding.ASCII.GetBytes(postString)
Client.UploadData(URL, "POST", postBytes)
A simplified version of my URL is as follows:
"../REST/1.0/ticket/" + ticketNumber + "/comment?user=" + userName + "&pass=" + password
Finally, an example of the content that I post is:
postString = "content=Text: RT Test" + vbLf + "Action: Comment" + vbLf + "Attachment: examplefile.jpg" + vbLf + "attachment_1="
As you can see, the postString is converted to bytes and then uploaded to the server. However, I do not know where or how I should be posting the raw attachment itself. The documentation for the service we are specifically using states to use a variable "attachment_1," which I added to the postString variable, but I am not sure what the next step should be. Should the file be converted into bytes and appended to the postBytes variable? I attempted something like this but I received an error saying that no attachment was found for examplefile.jpg.
Thanks for your help!
We could not use Client.UploadData(...) and had to convert the entire post to bytes, starting with the POST fields before the attachment, then the attachment itself, and finally the remainder of the POST fields.
Public Sub AddAttachmentToRT(ByVal url As String, ByVal fileName As String, ByVal filePath As String)
Dim dataBoundary As String = "--xYzZY"
Dim request As HttpWebRequest
Dim fileType As String = "image/jpeg" 'Will want to extract this to make it more generic from the uploaded file.
'Create a POST web request to the REST interface using the passed URL
request = CType(WebRequest.Create(url), HttpWebRequest)
request.ContentType = "multipart/form-data; boundary=xYzZY"
request.Method = "POST"
request.KeepAlive = True
'Write the request to the requestStream
Using requestStream As IO.Stream = request.GetRequestStream()
'Create a variable "attachment_1" in the POST, specify the file name and file type
Dim preAttachment As String = dataBoundary + vbCrLf _
+ "Content-Disposition: form-data; name=""attachment_1""; filename=""" + fileName + """" + vbCrLf _
+ "Content-Type: " + fileType + vbCrLf _
+ vbCrLf
'Convert this preAttachment string to bytes
Dim preAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(preAttachment)
'Write this preAttachment string to the stream
requestStream.Write(preAttachmentBytes, 0, preAttachmentBytes.Length)
'Write the file as bytes to the stream by passing its exact location
Using fileStream As New IO.FileStream(Server.MapPath(filePath + fileName), IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(4096) As Byte
Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
Do While (bytesRead > 0)
requestStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
Loop
End Using
'Create a variable named content in the POST, specify the attachment name and comment text
Dim postAttachment As String = vbCrLf _
+ dataBoundary + vbCrLf _
+ "Content-Disposition: form-data; name=""content""" + vbCrLf _
+ vbCrLf _
+ "Action: comment" + vbLf _
+ "Attachment: " + fileName + vbCrLf _
+ "Text: Some description" + vbCrLf _
+ vbCrLf _
+ "--xYzZY--"
'Convert postAttachment string to bytes
Dim postAttachmentBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postAttachment)
'Write the postAttachment string to the stream
requestStream.Write(postAttachmentBytes, 0, postAttachmentBytes.Length)
End Using
Dim response As Net.WebResponse = Nothing
'Get the response from our REST request to RT
'Required to capture response, without this Try-Catch attaching will fail
Try
response = request.GetResponse()
Using responseStream As IO.Stream = response.GetResponseStream()
Using responseReader As New IO.StreamReader(responseStream)
Dim responseText = responseReader.ReadToEnd()
End Using
End Using
Catch exception As Net.WebException
response = exception.Response
If (response IsNot Nothing) Then
Using reader As New IO.StreamReader(response.GetResponseStream())
Dim responseText = reader.ReadToEnd()
End Using
response.Close()
End If
Finally
request = Nothing
End Try
End Sub