asp.net如何发起post请求?

api工厂
业界专家认证
2015-03-03 01:03:52
以前用 asp 的时候,程序是这么写的:


<%
dim XmlHttp
set XmlHttp=Server.createobject("Microsoft.XMLHTTP")
XmlHttp.open "POST","http://www.s2m.cc/rest/ip/getAddress/?token=03b92716-d6f9-490f-86c1-c4c017832083&ip=202.101.172.46",false
XmlHttp.send "token=03b92716-d6f9-490f-86c1-c4c017832083&ip=202.101.172.46"
response.write bytesToBSTR(XmlHttp.responseBody,"utf-8")

Function BytesToBstr(Body,Cset)
Dim Objstream
Set Objstream =Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>



这个写法如果换成 asp.net 应该怎么写呢?
...全文
403 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
前台使用ajax,后台 httprequest
rayyu1989 2015-03-04
  • 打赏
  • 举报
回复
WebClient .UploadString https://technet.microsoft.com/zh-CN/library/ms144236
king4323210 2015-03-04
  • 打赏
  • 举报
回复
后台代码用 HttpRequest js代码用jquery $.ajax({ type:post})
月之点点 2015-03-04
  • 打赏
  • 举报
回复
strPostdata = strPostdata.FCoding(1) .Replace("%3d", "=") .Replace("%26", "&") .Replace("%3f", "?") .Replace("%3a", ":") .Replace("%2f", "/"); 这块可以去掉。我是为了防止中文乱码才加上的
月之点点 2015-03-04
  • 打赏
  • 举报
回复

        ///<summary>
        ///【POST】获取网站的html
        ///</summary>
        ///<param name="URL">url地址</param>
        ///<param name="strPostdata">发送的数据,格式:id=1&idd=2&type=3</param>
        ///<param name="Enco">编码格式,例如 utf-8【默认】,gb2312</param>
        ///<returns></returns>
        public static string PostWebHtml(string URL, string strPostdata, string Enco = "UTF-8")
        {
            strPostdata = strPostdata.FCoding(1)
                .Replace("%3d", "=")
                .Replace("%26", "&")
                .Replace("%3f", "?")
                .Replace("%3a", ":")
                .Replace("%2f", "/");

            Encoding encoding = Encoding.Default;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = "post";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";
            byte[] buffer = encoding.GetBytes(strPostdata);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(Enco)))
            {
                return reader.ReadToEnd();
            }
        }
三楼の郎 2015-03-03
  • 打赏
  • 举报
回复

Dim data As Byte()
data = System.Text.Encoding.UTF8.GetBytes("token=03b92716-d6f9-490f-86c1-c4c017832083&ip=202.101.172.46“)

Dim myReq As System.Net.HttpWebRequest = System.Net.WebRequest.Create(”http://www.s2m.cc/rest/ip/getAddress/“)
myReq.Method = "POST"
myReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"
myReq.ContentLength = data.Length

Dim myStream As System.IO.Stream = myReq.GetRequestStream
myStream.Write(data, 0, data.Length)
myStream.Close()

Dim myResponse As System.Net.HttpWebResponse = myReq.GetResponse
myStream = myResponse.GetResponseStream
Dim myStreamReader As New System.IO.StreamReader(myStream)
Response.Write(myStreamReader.ReadToEnd)

myStreamReader.Close()
myStreamReader = Nothing
myStream.Close()
myStream=Nothing
myReq = Nothing
by_封爱 2015-03-03
  • 打赏
  • 举报
回复

public static string Post(string Url, string Data)
        {
            string result = "";
            WebClient myClient = new WebClient();
            myClient.Headers.Add("U-ApiKey", "XOO");
            myClient.Encoding = System.Text.Encoding.UTF8;
            try
            {
                result = myClient.UploadString(Url, Data);
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }
            return result;
        }
发送post并且获取返回值
  • 打赏
  • 举报
回复
<form action="post"> 或者 $.post

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧