在WinForm中发送HTTP请求的实现方法
程序员文章站
2022-05-28 18:45:03
如何在winform中请求发送http
手工发送http请求主要是调用 system.net的httpwebresponse方法
手工发送http的get请求:...
如何在winform中请求发送http
手工发送http请求主要是调用 system.net的httpwebresponse方法
手工发送http的get请求:
string strurl = "http://localhost/play/ch1/service1.asmx/dosearch?keyword="; strurl +=this.textbox1.text; system.net.httpwebrequest request; // 创建一个http请求 request = (system.net.httpwebrequest)webrequest.create(strurl); //request.method="get"; system.net.httpwebresponse response; response = (system.net.httpwebresponse)request.getresponse(); system.io.stream s; s = response.getresponsestream(); xmltextreader reader = new xmltextreader(s); reader.movetocontent(); string strvalue = reader.readinnerxml(); strvalue = strvalue.replace("<","<"); strvalue = strvalue.replace(">",">"); messagebox.show(strvalue); reader.close();
手工发送http的post请求
string strurl = "http://localhost/play/ch1/service1.asmx/dosearch"; system.net.httpwebrequest request; request = (system.net.httpwebrequest)webrequest.create(strurl); //post请求方式 request.method="post"; // 内容类型 request.contenttype="application/x-www-form-urlencoded"; // 参数经过url编码 string paraurlcoded = system.web.httputility.urlencode("keyword"); paraurlcoded += "=" + system.web.httputility.urlencode(this.textbox1.text); byte[] payload; //将url编码后的字符串转化为字节 payload = system.text.encoding.utf8.getbytes(paraurlcoded); //设置请求的 contentlength request.contentlength = payload.length; //获得请 求流 stream writer = request.getrequeststream(); //将请求参数写入流 writer.write(payload,0,payload.length); // 关闭请求流 writer.close(); system.net.httpwebresponse response; // 获得响应流 response = (system.net.httpwebresponse)request.getresponse(); system.io.stream s; s = response.getresponsestream(); xmltextreader reader = new xmltextreader(s); reader.movetocontent(); string strvalue = reader.readinnerxml(); strvalue = strvalue.replace("<","<"); strvalue = strvalue.replace(">",">"); messagebox.show(strvalue); reader.close();
以上这篇在winform中发送http请求的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: asp 防止SQL注入代码
下一篇: C#递归算法之分而治之策略