日期:2014-05-17  浏览次数:21473 次

winform接收http请求数据
winform如何接收其他方http请求(get ,post)数据?

------解决方案--------------------
winform接收请求?是想用winform做httpserver??
------解决方案--------------------
HttpWebRequest 
------解决方案--------------------
引用:
引用:
winform接收请求?是想用winform做httpserver??
是这个意思,提供给其他方http://10.1.1.2:89 或 http://10.1.1.2:89/test,类似这样的,供其他程序post数据

http://www.cnblogs.com/anmoon/archive/2010/08/22/1805670.html
------解决方案--------------------
public static string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            //request.CookieContainer = Cookie;
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            //response.Cookies = Cookie.GetCookies(response.ResponseUri);
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

        public static string  HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?" + postDataStr));
            request.Method = "GET";
            r