C#(Winform) Http 发送数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#(Winform) Http 发送数据相关的知识,希望对你有一定的参考价值。

Get方式

 private string HttpGet(string url, string postData) {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + (postData == "" ? "" : "?") + postData);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }

Post方式

private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";

            byte[] data = Encoding.GetEncoding("utf-8").GetBytes(postDataStr);

            request.ContentLength = data.Length;

            Stream myRequestStream = request.GetRequestStream();

            myRequestStream.Write(data,0,data.Length);
            myRequestStream.Close();

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

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

            return retString;
        }

 

以上是关于C#(Winform) Http 发送数据的主要内容,如果未能解决你的问题,请参考以下文章

C# POst 接收或发送XML

c#使用多线程发送HTTP请求的问题,高手进!~~~

c# winform开发,客户端和服务器端如何理解

C# winform 中如何实现如下的表格

使用 Outlook 2010 和 2013 C# 从 winform 实现发送邮件

C# Winform 想做一个CSV传入数据库的进度条,怎么做?