【问题标题】:HTTP Post request from a ASP.NET button来自 ASP.NET 按钮的 HTTP Post 请求
【发布时间】:2015-10-12 19:52:35
【问题描述】:
我正在尝试从一个简单的 ASP.NET 网站调用 Azure 自动化 Runbook,要调用此 Runbook,我必须从我的网站创建一个简单的 HTTP POST。
我的想法是创建一个简单的按钮,让 onclick 发送这个 HTTP 请求。
我该怎么做?
【问题讨论】:
标签:
asp.net-mvc
azure
httprequest
webhooks
azure-automation
【解决方案1】:
在您的简单网站中,在您的 ASPX 页面上放置一个 ASP 按钮(使用 runat="server",以便它回调您的服务器)。
<asp:Button ID="Submit" runat="server" Text="Submit" />
并将您的代码放入您的回调代码中的按钮回调过程(本示例中为旧样式 .aspx.vb),以将帖子发送到不同的站点。
Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
'Build the post data string to send
Dim PostData As String
PostData = PostData & "YourValueName1=" & System.Web.HttpUtility.UrlEncode(YourValuetoSend1) & "&"
PostData = PostData & "YourValueName2=" & System.Web.HttpUtility.UrlEncode(YourValuetoSend2)
'Convert the string to byte array for posting
Dim enc As New System.Text.UTF8Encoding
Dim postdatabytes As Byte()
postdatabytes = enc.GetBytes(PostData)
'send it
Dim web As System.Net.HttpWebRequest
web = System.Net.WebRequest.Create("http://www.Your_Azure_Site_to_Post_To.com/subdirectory/pagename.aspx")
web.Method = "POST"
web.ContentType = "application/x-www-form-urlencoded"
web.ContentLength = postdatabytes.Length
Using stream = web.GetRequestStream()
stream.Write(postdatabytes, 0, postdatabytes.Length)
stream.Close()
End Using
Dim result As System.Net.WebResponse
result = web.GetResponse()
End Sub