问题描述
以前用asp的时候,程序是这么写的:<%dimXmlHttpsetXmlHttp=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",falseXmlHttp.send"token=03b92716-d6f9-490f-86c1-c4c017832083&ip=202.101.172.46"response.writebytesToBSTR(XmlHttp.responseBody,"utf-8")FunctionBytesToBstr(Body,Cset)DimObjstreamSetObjstream=Server.CreateObject("adodb.stream")objstream.Type=1objstream.Mode=3objstream.Openobjstream.Writebodyobjstream.Position=0objstream.Type=2objstream.Charset=CsetBytesToBstr=objstream.ReadTextobjstream.Closesetobjstream=nothingEndFunction%>
这个写法如果换成asp.net应该怎么写呢?
解决方案
解决方案二:
<formaction="post">或者$.post
解决方案三:
publicstaticstringPost(stringUrl,stringData){stringresult="";WebClientmyClient=newWebClient();myClient.Headers.Add("U-ApiKey","XOO");myClient.Encoding=System.Text.Encoding.UTF8;try{result=myClient.UploadString(Url,Data);}catch(Exceptionex){result=ex.Message;}returnresult;}
发送post并且获取返回值
解决方案四:
DimdataAsByte()data=System.Text.Encoding.UTF8.GetBytes("token=03b92716-d6f9-490f-86c1-c4c017832083&ip=202.101.172.46“)DimmyReqAsSystem.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.LengthDimmyStreamAsSystem.IO.Stream=myReq.GetRequestStreammyStream.Write(data,0,data.Length)myStream.Close()DimmyResponseAsSystem.Net.HttpWebResponse=myReq.GetResponsemyStream=myResponse.GetResponseStreamDimmyStreamReaderAsNewSystem.IO.StreamReader(myStream)Response.Write(myStreamReader.ReadToEnd)myStreamReader.Close()myStreamReader=NothingmyStream.Close()myStream=NothingmyReq=Nothing
解决方案五:
解决方案六:
///<summary>///【POST】获取网站的html///</summary>///<paramname="URL">url地址</param>///<paramname="strPostdata">发送的数据,格式:id=1&idd=2&type=3</param>///<paramname="Enco">编码格式,例如utf-8【默认】,gb2312</param>///<returns></returns>publicstaticstringPostWebHtml(stringURL,stringstrPostdata,stringEnco="UTF-8"){strPostdata=strPostdata.FCoding(1).Replace("%3d","=").Replace("%26","&").Replace("%3f","?").Replace("%3a",":").Replace("%2f","/");Encodingencoding=Encoding.Default;HttpWebRequestrequest=(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);HttpWebResponseresponse=(HttpWebResponse)request.GetResponse();using(StreamReaderreader=newStreamReader(response.GetResponseStream(),System.Text.Encoding.GetEncoding(Enco))){returnreader.ReadToEnd();}}
解决方案七:
strPostdata=strPostdata.FCoding(1).Replace("%3d","=").Replace("%26","&").Replace("%3f","?").Replace("%3a",":").Replace("%2f","/");这块可以去掉。我是为了防止中文乱码才加上的
解决方案八:
后台代码用HttpRequestjs代码用jquery$.ajax({type:post})
解决方案九:
WebClient.UploadStringhttps://technet.microsoft.com/zh-CN/library/ms144236
解决方案十:
前台使用ajax,后台httprequest