2016-08-05 47 views
0

這是我第一次使用JSON,並且卡住了。我試圖通過http://sms.roamtech.com/smsapi/發送測試短信。格式爲:使用Newtonsoft在VB.net中編寫和讀取JSON

發送消息格式(json)。

{ 
    "result":{ 
     "account":"xxxx", 
     "user":"xxxx", 
     "password":"xxxxxxxx", 
     "requestType":"BULK", 
     "alphanumeric":"xxxxxxxx", 
     "data":{ 
      "linkid":"xxxxxxx", 
      "msisdn":"xxxxxxxxxxx", 
      "networkid":"1", 
      "message":"xxxxxxxxxxxx", 
      "callback":"http//test" 
     } 
    } 
} 

所以,這是我想出審查就這個問題和其他網站不同的崗位後:

Imports Newtonsoft.Json 
Imports Newtonsoft.Json.Linq 
Imports System.IO 
Imports System.Net 
Imports System.Text 

Module modJSON 
    Public Class clsResult 
     Public account As String 
     Public user As String 
     Public password As String 
     Public requestType As String 
     Public alphanumeric As String 
     Public data As New clsData 
    End Class 
    Public Class clsData 
     Public linkid As String 
     Public msisdn As String 
     Public networkid As String 
     Public message As String 
     Public callback As String 
    End Class 
    Public Class clsPOST 
     Public result As New clsResult 

    End Class 

    Public Sub chkJSON() 
     Dim r As New clsResult 
     Dim d As New clsData 
     Dim x As New clsPOST 

     r.account = "8852" 
     r.user = "username" 
     r.password = "password" 
     r.requestType = "BULK" 
     r.alphanumeric = "SMSLEO" 


     d.linkid = "1001" 
     d.msisdn = "2547xxxxxxxx" 
     d.networkid = "1" 
     d.message = "Just a test" 
     d.callback = "http://infiniti-africa.com/json" 

     r.data = d 
     x.result = r 

     Dim uriRoam As New Uri("http://sms.roamtech.com/smsapi") 
     Dim strJSON = JsonConvert.SerializeObject(x, Formatting.Indented) 
     Dim bytJSON = Encoding.UTF8.GetBytes(strJSON) 

     Dim result_post = SendRequest(uriRoam, bytJSON, "application/json", "POST") 
     MsgBox(result_post) 

    End Sub 

    Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String 
     Dim req As WebRequest = WebRequest.Create(uri) 
     req.ContentType = contentType 
     req.Method = method 
     req.ContentLength = jsonDataBytes.Length 


     Dim stream = req.GetRequestStream() 
     stream.Write(jsonDataBytes, 0, jsonDataBytes.Length) 
     stream.Close() 

     Dim response = req.GetResponse().GetResponseStream() 

     Dim reader As New StreamReader(response) 
     Dim res = reader.ReadToEnd() 
     reader.Close() 
     response.Close() 

     Return res 
    End Function 
End Module 

字符串strJSON似乎包含正確的密鑰:值組合。但是,該代碼不會發送測試SMS,我也沒有得到任何迴應。 'SendRequest'返回一個空字符串。

此外,我不確定使用「回調」網址的哪個地方,這是傳遞報告的地方。

注: 1. "linkid"是一個唯一的消息ID 2. "msidn"被接收方電話號碼

任何幫助理解。

我也用下面的類的嘗試:

Public Class JsonPost 

    Private urlToPost As String = "" 

    Public Sub New(ByVal urlToPost As String) 
     Me.urlToPost = urlToPost 
    End Sub 

    Public Function postData(pstData As Byte()) As Boolean 
     Dim webClient As New WebClient() 
     Dim resByte As Byte() 
     Dim resString As String 

     Try 
      webClient.Headers("content-type") = "application/json" 
      resByte = webClient.UploadData(Me.urlToPost, "post", pstData) 
      resString = Encoding.Default.GetString(resByte) 
      Console.WriteLine(resString) 
      webClient.Dispose() 
      Return True 
     Catch ex As Exception 
      Console.WriteLine(ex.Message) 
     End Try 
     Return False 
    End Function 

End Class 

然後美其名曰:

Dim strJSON = JsonConvert.SerializeObject(x) 
    Dim bytJSON = Encoding.UTF8.GetBytes(strJSON) 

    Dim jsonPost As New JsonPost("http://sms.roamtech.com/smsapi") 
    jsonPost.postData(bytJSON) 

我仍然得到什麼。現在已經三天苦苦掙扎了。任何想法的人?

回答