如何实现HTTP请求

2020-10-28 教育 59阅读
参数:url:目标地址data:要post的数据比如a=1&b=2publicstringGetPostString(stringurl,stringdata){try{byte[]postBytes=Encoding.GetEncoding("utf-8").GetBytes(data);HttpWebRequestmyRequest=(HttpWebRequest)WebRequest.Create(url);myRequest.Method="POST";myRequest.ContentType="text/html";myRequest.ContentLength=postBytes.Length;myRequest.Proxy=null;StreamnewStream=myRequest.GetRequestStream();newStream.Write(postBytes,0,postBytes.Length);newStream.Close();//GetresponseHttpWebResponsemyResponse=(HttpWebResponse)myRequest.GetResponse();using(StreamReaderreader=newStreamReader(myResponse.GetResponseStream(),Encoding.GetEncoding("utf-8"))){stringcontent=reader.ReadToEnd();returncontent;}}catch(System.Exceptionex){returnex.Message;}}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com