开发者

Please fix this vb.net string

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myRedirectUri As Uri
    开发者_StackOverflow中文版    myRedirectUri = myRedirectUri.Replace(vbCrLf, "")
        Me.Response.Redirect("http://softmania.in/Services/sms/sms?submit=submit&from=" & TextBox3.Text.ToString & "&" & "recipients=" & TextBox1.Text.Trim & "&" & "msg=" & TextBox2.Text.ToString & "&" & "Sendnow=" & "Send + Now")
End Sub

i have a textbox2.text is multiline ...

when i type the msgs in textbox2 in multiline then it shows the error msg ..

Redirect URI cannot contain newline characters.


URLs/URIs can't contain more than a single line. For instance, this wouldn't be a valid URL:

http://www.yahoo.com/
some/
news.html

But this is what's happening because you have text coming in from a multiline textbox.

There's also another issue that this exposes, which is that you have to encode any text that you put into your URL. Otherwise, there's nothing stopping the user from entering something like "asdfasdfsdf&msg=zcxvzxcv&command=delete_everything", which when typed into your textbox, would yield http://softmania.in/Services/sms/sms?submit=submit&from=asdfasdfsdf&msg=zcxvzxcv&command=delete_everything -- and you certainly don't want that.

Special characters like & and ? and = and space and newline need to be "escaped", which you do using a function like UrlEncode, like this:

Me.Response.Redirect("http://softmania.in/Services/sms/sms?submit=submit&from=" & HttpUtility.UrlEncode(TextBox3.Text.ToString) & "&" & "recipients=" & HttpUtility.UrlEncode(TextBox1.Text.Trim) & "&" & "msg=" & HttpUtility.UrlEncode(TextBox2.Text.ToString) & "&" & "Sendnow=" & "Send + Now")


You won't run into issues just from newline characters - any special characters in the text boxes (like an ampersand) will break this request. Instead, you should escape the parameter values with Uri.EscapeDataString():

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim from As String = Uri.EscapeDataString(TextBox3.Text.ToString)
    Dim to As String = Uri.EscapeDataString(TextBox1.Text.Trim)
    Dim msg As String = Uri.EscapeDataString(TextBox2.Text.ToString)
    Dim myRedirectUri As String = "http://softmania.in/Services/sms/sms?submit=submit&from=" & from _
        & "&recipients=" & to & "&msg=" & msg _
        & "&Sendnow=" & "Send + Now"
    Me.Response.Redirect(myRedirectUri)
End Sub


You may also want to consider using the StringBuilder class instead of all those string concatenations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜