【问题标题】:Problem with body while sending a pushbullet notification发送推送通知时出现正文问题
【发布时间】:2021-02-01 17:58:27
【问题描述】:

我在发送 PushBullet 通知时遇到了一个奇怪的问题。 如果富文本框包含如下文本,我可以发送通知:

这是文字

如果richtextbox 是,它不会发送任何内容:

这个
是文字

或:

这个

文字

我尝试使用正则表达式替换来删除所有空格,但它不起作用。我用于发送通知的代码:

Private Sub PushBullet()

    'PUSHBULLET
    Dim token As String = "pushbullet token"            
    Dim title As String = "Notication Title"                             
    Dim body As String
    body = Regex.Replace(RichTextBox1.Text, "^\s+$[\r\n]*", "", RegexOptions.Multiline)
    

    Try
        ' Create a request using a URL that can receive a post.'
        Dim Request As HttpWebRequest = CType(WebRequest.Create("https://api.pushbullet.com/v2/pushes"), HttpWebRequest)

        ' 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 = "{""type"": ""note"", ""title"": """ & title & """, ""body"": """ & body & """}"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)

        ' Set the ContentLength property of the WebRequest.'
        Request.ContentLength = byteArray.Length
        ' Set the ContentType property of the WebRequest.'
        Request.ContentType = "application/json"
        ' Add the token to header.'
        Request.Headers.Add("Access-Token", token)
        ' 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.'
        dataStream.Close()
        ' Get the response.'
        Dim response As WebResponse = Request.GetResponse()
        ' 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()
        ' Show result in RichTextBox.'
        '  RichTextBox1.Text = responseFromServer
        ' Clean up the streams.'
        reader.Close()
        dataStream.Close()
        response.Close()
    Catch ex As Exception
    End Try
End Sub

有什么想法吗? 谢谢

【问题讨论】:

  • 流需要被处理。使用Using 块。

标签: vb.net pushbullet


【解决方案1】:

我解决了:

   TextBox1.Text = TextBox1.Text.Replace(vbLf, " ").Replace(vbCr, "")

【讨论】:

  • 如果要保持多行布局,用\n和\r替换TextBox1.Text = TextBox1.Text.Replace(vbLf, "\n").Replace(vbCr, "\r")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-29
  • 2010-11-30
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多