如何在.net vb中发送POST? [英] How to send a POST in .net vb?

查看:116
本文介绍了如何在.net vb中发送POST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是让我的用户输入电话号码和消息,然后将其发布到发送消息的文本营销人员.

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

但是,我不想将用户发送到那里.我要做的就是将数据发布到url,仅此而已,并且用户停留在当前页面上.

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.

有帮助吗?

推荐答案

实际上,您不必执行此服务器端(vb),只需纯html即可完成操作:

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>

这会将数据(实际上是重定向)发布到google.com.

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

也许您可以使用客户端脚本(jQuery)-$ .ajax()或$ .post().但我认为您将面临跨域限制(有一种解决方法,但它不是那么干净直接).

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

另一个正在使用HttpWebRequest类.这是服务器端,该帖子将来自您的服务器而不是客户端(如第一种方法将执行的操作).调用request.GetResponse()后,您可以从远程服务器检索输出并将其呈现在页面上.但是,如果您要发布并重定向到远程URL,那么我想您应该使用第一种方法.

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.

在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:

在VB.net中使用HttpWebRequest的GET请求.

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

这篇关于如何在.net vb中发送POST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆