一个简单的Httpserver以及获取post提交的参数
来源:互联网 发布:女狙击手知乎 编辑:程序博客网 时间:2024/05/17 22:47
以下代码是我从网上找来的,但是一直获取不到post提交的参数,最后经过我的修改,终于可以得到post提交的数据。因为本人在网上找了很久都没有找到相关的资料,特意发出来希望能帮到大家,有什么不足的地方还请大神们指正,小弟不胜感激。
Httpserver代码
1 public void StartListen() 2 { 3 using (HttpListener listerner = new HttpListener()) 4 { 5 listerner.AuthenticationSchemes = AuthenticationSchemes.Anonymous;//指定身份验证 Anonymous匿名访问 6 listerner.Prefixes.Add("http://localhost:8080/web/"); 7 8 // listerner.Prefixes.Add("http://localhost/web/"); 9 listerner.Start();10 Console.WriteLine("WebServer Start Successed.......");11 while (true)12 {13 //等待请求连接14 //没有请求则GetContext处于阻塞状态15 HttpListenerContext ctx = listerner.GetContext();16 ctx.Response.StatusCode = 200;//设置返回给客服端http状态代码17 //RequestType=1&Longitude=0.0&Latitude=0.0&RequestSource=xx18 string RequestType = "";19 string Longitude = "";20 string Latitude = "";21 string RequestSource = "";22 //获取客户端写入的信息23 string strRequest = ShowRequestData(ctx.Request);24 if (!string.IsNullOrEmpty(strRequest))25 {26 string[] strR = strRequest.Split('&');27 string[] strP = new string[strR.Length];28 for (int i = 0; i < strR.Length; i++)29 {30 strP[i] = strR[i].Split('=')[1];31 }32 RequestType = strP[0];33 Longitude = strP[1];34 Latitude = strP[2];35 RequestSource = strP[3];36 }37 string strSend = "";38 39 if (!string.IsNullOrEmpty(RequestType))40 {41 if (RequestType == "1" && !string.IsNullOrEmpty(Longitude) && !string.IsNullOrEmpty(Latitude))42 {43 strSend = GetPointWeather(Longitude, Latitude, RequestSource);44 }45 if (RequestType == "2")46 { 47 48 }49 }50 51 //使用Writer输出http响应代码52 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.GetEncoding("gb2312")))53 {54 writer.WriteLine(strSend);55 writer.Close();56 ctx.Response.Close();57 }58 59 }60 //listerner.Stop();61 }62 }
GetPointWeather函数是我自己定义的,主要是得到返回给客户端的信息。
获取客户端的输入流
1 public string ShowRequestData(HttpListenerRequest request) 2 { 3 if (!request.HasEntityBody) 4 { 5 Console.WriteLine("No client data was sent with the request."); 6 return ""; 7 } 8 System.IO.Stream body = request.InputStream; 9 System.Text.Encoding encoding = request.ContentEncoding;10 System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);11 if (request.ContentType != null)12 {13 Console.WriteLine("Client data content type {0}", request.ContentType);14 }15 Console.WriteLine("Client data content length {0}", request.ContentLength64);16 17 Console.WriteLine("Start of client data:");18 // Convert the data to a string and display it on the console.19 string s = reader.ReadToEnd();20 Console.WriteLine(s);21 Console.WriteLine("End of client data:");22 body.Close();23 reader.Close();24 // If you are finished with the request, it should be closed also.25 return s;26 }
post请求代码:
1 /// <summary> 2 /// 使用Http请求天气信息 3 /// </summary> 4 /// <param name="RequestType">请求类别(1:点预报 2:镇预报)</param> 5 /// <param name="Longitude">经度(如果是镇预报请求 值为空)</param> 6 /// <param name="Latitude">纬度(如果是镇预报请求 值为空)</param> 7 /// <param name="RequestSource">请求来源</param> 8 public void GetWeatherByHttp(string RequestType, string Longitude, string Latitude, string RequestSource) 9 {10 string strURL = "http://localhost:8080/web/";11 System.Net.HttpWebRequest request;12 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);13 //Post请求方式 14 request.Method = "POST";15 //内容类型 16 request.ContentType = "application/x-www-form-urlencoded";17 //参数经过URL编码 18 Encoding myEncoding = Encoding.GetEncoding("gb2312");//GetEncoding("gb2312")19 string paraUrlCoded = HttpUtility.UrlEncode("RequestType", myEncoding);20 paraUrlCoded += "=" + HttpUtility.UrlEncode(RequestType, myEncoding);21 paraUrlCoded += "&" + HttpUtility.UrlEncode("Longitude", myEncoding);22 paraUrlCoded += "=" + HttpUtility.UrlEncode(Longitude, myEncoding);23 paraUrlCoded += "&" + HttpUtility.UrlEncode("Latitude", myEncoding);24 paraUrlCoded += "=" + HttpUtility.UrlEncode(Latitude, myEncoding);25 paraUrlCoded += "&" + HttpUtility.UrlEncode("RequestSource", myEncoding);26 paraUrlCoded += "=" + HttpUtility.UrlEncode(RequestSource, myEncoding);27 byte[] payload;28 //将URL编码后的字符串转化为字节 29 payload = System.Text.Encoding.ASCII.GetBytes(paraUrlCoded);30 //设置请求的ContentLength 31 request.ContentLength = payload.Length;32 //获得请求流 33 Stream writer = request.GetRequestStream();34 //将请求参数写入流 35 writer.Write(payload, 0, payload.Length);36 //关闭请求流 37 writer.Close();38 System.Net.HttpWebResponse response;39 //获得响应流 40 response = (System.Net.HttpWebResponse)request.GetResponse();41 System.IO.Stream s;42 s = response.GetResponseStream();43 string StrDate = "";44 string strValue = "";45 StreamReader Reader = new StreamReader(s, Encoding.Default);46 while ((StrDate = Reader.ReadLine()) != null)47 {48 strValue += StrDate + "\r\n";49 }50 // txtInfo.Text = strValue; 51 Reader.Close();52 }
阅读全文
0 0
- 一个简单的Httpserver以及获取post提交的参数
- 一个简单的Httpserver以及获取post提交的参数
- socket实现一个简单的httpserver
- nodejs搭建一个简单的httpserver
- 简单的HttpServer
- HttpServer的简单运用
- flask 获取 get post 请求提交的参数
- 简单的POST提交方法
- 一个简单的httpServer(STL编写,控制台程序)
- PHP post提交xml 以及获取post过来的xml数据
- 简单的Httpserver,Httpclient,xml
- HttpServer的简单实现_Java
- 在ASP.NET中获取参数POST和GET方式提交的参数
- 在ASP.NET中获取参数POST和GET方式提交的参数
- 在ASP.NET中获取参数POST和GET方式提交的参数
- PostMethod获取post提交的页面代码
- struts2 解决浏览器post提交值Actionz还能获取第一个参数的问题
- post提交的数据有哪几种编码格式?能否通过URL参数获取用户账户密码
- 自定义VideoView实现视频画面的缩放
- 'getDrawable(int)' is deprecated,getDrawable过时
- git上传代码至github
- 代码优化, volatile关键字
- input.py
- 一个简单的Httpserver以及获取post提交的参数
- Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
- Bat命令学习
- thinkphp关于考生信息相关统计
- Windows下Python3.5安装Numpy、Scipy
- Java学习笔记(二十七):阿里巴巴java开发手册阅读笔记
- 实现无限极分类的一个类、以及统计每个等级有多少的个数(存在父子级关系的字段)
- Ubuntu下安装及卸载JDK
- tomcat部署项目,如何去掉项目名