请修复此vb.net字符串

时间:2010-12-10 06:54:17

标签: asp.net vb.net

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myRedirectUri As Uri
        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

我有一个textbox2.text是多行...

当我在多行中的textbox2中键入msgs时,它会显示错误消息..

重定向URI不能包含换行符。

3 个答案:

答案 0 :(得分:2)

网址/ URI不能包含多行。例如,这不是有效的URL:

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

但这就是发生的事情,因为你有来自多行文本框的文字。

还有另一个问题,即您必须对您放入网址的任何文字进行编码。否则,没有什么能阻止用户输入类似“asdfasdfsdf& msg = zcxvzxcv& command = delete_everything”的东西,当输入到你的文本框中时,会产生http://softmania.in/Services/sms/sms?submit=submit&from=asdfasdfsdf&msg=zcxvzxcv&command=delete_everything - 你当然不希望这样。

特殊字符,如&和?和=和空格和换行符必须是“escaped”,你可以使用类似UrlEncode之类的函数执行此操作:

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")

答案 1 :(得分:1)

您不会仅仅因为换行符而遇到问题 - 文本框中的任何特殊字符(如&符号)都会破坏此请求。相反,您应该使用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

答案 2 :(得分:0)

您可能还想考虑使用StringBuilder class而不是所有字符串连接。