一个简单的Httpserver以及获取post提交的参数
以下代码是我从网上找来的,但是一直获取不到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=xx 18 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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。