简体   繁体   English

如何使用vb.net发送通知以进行解析

[英]How to send Notifications to Parse using vb.net

When I am trying to push the following JSON data it's showing "The remote server returned an error: (400) Bad Request". 当我尝试推送以下JSON数据时,它显示“远程服务器返回错误:(400)错误的请求”。 It succeeds only if the JSON data contents all are in single quotes. 仅当JSON数据内容全部用单引号引起来时,它才会成功。

    {  
   'mbs':{  
      'msg':{  
         'i':'6510',
         'c':'0',
         'st':'030116125200',
         'et':'030116135800',
         'rs':'0',
         'mt':'1',
         'r':'165908',
         'ms':'256',
         's':'1',
         'tr':'2',
         'l':'en-US',
         'mr':'0',
         'th':'green',
         'dl':'1',
         'h':'1',
         'd':[  
            {  
               'data':'<p><span style="font-family: Calibri; font-size: 14px;">Enter the content of your alert below. Click on Save to save your changes. When ready to publish the alert, click on Publish.</span></p>'
            },
            {  
               'data':'<p><span style="font-family: Calibri; font-size: 14px;">Enter the content of your alert below. Click on Save to save your changes. When ready to publish the alert, click on Publish.</span></p>'
            }
         ],
         't':[  
            {  
               'data':'English'
            },
            {  
               'data':'English'
            }
         ]
      }
   }
}

I used following vb.net code 我使用了以下vb.net代码

Private Function PushNotifications(ByVal PushMessage As String) As Boolean
    Dim isPushMessageSend As Boolean = False
    Dim postString As String = ""
    Dim urlpath As String = "https://api.parse.com/1/push"
    Dim httpWebRequest = DirectCast(WebRequest.Create(urlpath), HttpWebRequest)

    postString = "{ ""where"": {}, " & """data"" : {""alert"":""" & PushMessage & """}" & "}" 'for sending to everyone
    httpWebRequest.ContentType = "application/json"
    httpWebRequest.ContentLength = postString.Length
    httpWebRequest.Headers.Add("X-Parse-Application-Id", "*****")
    httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "*****")
    httpWebRequest.Method = "POST"
    Dim requestWriter As New StreamWriter(httpWebRequest.GetRequestStream())
    requestWriter.Write(postString)
    requestWriter.Close()
    Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
    Using streamReader = New StreamReader(httpResponse.GetResponseStream())
        Dim responseText = streamReader.ReadToEnd()

        Dim jobjRes As JObject = JObject.Parse(responseText)
        If Convert.ToString(jobjRes).IndexOf("true") <> -1 Then
            isPushMessageSend = True
        End If
        System.IO.File.WriteAllText("D:\jsondata4.txt", jobjRes.ToString())
    End Using
    MessageBox.Show("Successfully sent")
    Return isPushMessageSend
End Function

That's because you have values that contain double quotes. 那是因为您的值包含双引号。 This works: 这有效:

'data':'<p><span style="font-family: Calibri; font-size: 14px;">'

This won't: 这不会:

"data":"<p><span style="font-family: Calibri; font-size: 14px;">"

because it's parsed as "data":"<p><span style=" 因为它被解析为"data":"<p><span style="

and then what follows, font-family: Calibri; font-size: 14px;">" 然后是font-family: Calibri; font-size: 14px;">" font-family: Calibri; font-size: 14px;">" isn't valid JSON. font-family: Calibri; font-size: 14px;">"不是有效的JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM