Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

asp.net - How to send a POST in .net vb?

What i am trying to do is get my user to enter in a phone number and message and then post it to text marketer which send the message.

at the moment if i use a response.redirect the message sense..

response.redirect("http://www.textmarketer.biz/gateway/?username=*****&password=*****&message=test+message&orig=test&number=447712345678")

However, i do not want to send the user there. all i want to do it post the data to the url and that's all for now and the user stay on the current page.

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

actually, you don't have to do this server side (vb), just plain html will do the trick:

<html>
    <body>
        <form action="http://google.com" method="post">
            <input type="hidden" value="somevalue"/>
            <input Type="submit" value="Submit"/>
        </form>
    </body>
</html>

this will post the data (and in effect, redirect) to google.com.

Maybe you could use client script (jQuery) - $.ajax() or $.post(). but I think you will face cross domain restrictions (there is a workaround but its not that clean and straightforward).

Another is using the HttpWebRequest class. This is server side and the post will originate from your server instead of the client (as what the 1st approach will do). upon calling request.GetResponse(), you can retrieve the output from the remote server and render it on your page. But if you want to post and redirect to the remote url then I guess you should use the first approach.

EDIT:

try this in VB:

Option Infer On
Imports System.Net
Imports System.Text


Public Class Test

    Private Sub TESTRUN()
        Dim s As HttpWebRequest
        Dim enc As UTF8Encoding
        Dim postdata As String
        Dim postdatabytes As Byte()
        s = HttpWebRequest.Create("http://www.textmarketer.biz/gateway/")
        enc = New System.Text.UTF8Encoding()
        postdata = "username=*****&password=*****&message=test+message&orig=test&number=447712345678"
        postdatabytes = enc.GetBytes(postdata)
        s.Method = "POST"
        s.ContentType = "application/x-www-form-urlencoded"
        s.ContentLength = postdatabytes.Length

        Using stream = s.GetRequestStream()
            stream.Write(postdatabytes, 0, postdatabytes.Length)
        End Using
        Dim result = s.GetResponse()
    End Sub
End Class

update2:

a GET request using HttpWebRequest in VB.net.

Dim s As HttpWebRequest
Dim username = "username=" + HttpUtility.UrlEncode("yourusername")
Dim password = "password=" + HttpUtility.UrlEncode("yourp@assword)!==&@(*#)!@#(_")
Dim message = "message=" + HttpUtility.UrlEncode("yourmessage")
Dim orig = "orig=" + HttpUtility.UrlEncode("dunno what this is")
Dim num = "number=" + HttpUtility.UrlEncode("123456")
Dim sep = "&"
Dim sb As New StringBuilder()
sb.Append(username).Append(sep).Append(password).Append(sep)
sb.Append(message).Append(sep).Append(orig).Append(sep).Append(num)

s = HttpWebRequest.Create("http://www.textmarketer.biz/gateway/?" + sb.ToString())

s.Method = "GET"
Dim result = s.GetResponse()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...