HTTP请求接口方法(POST/GET)

原创 2017年05月24日 15:24:41
 private 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";
            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;
        }
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

JAVA发送http get/post请求,调用http接口、方法

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import ja...

JAVA发送http get/post请求,调用http接口、方法

三个例子 —JAVA发送http get/post请求,调用http接口、方法例1:使用 HttpClient (commons-httpclient-3.0.jar jar下载地址:h...

HTTP协议中POST、GET、HEAD、PUT等请求方法及相应值得含义

HTTP协议中POST、GET、HEAD、PUT等请求方法及相应值得含义请求方法是请求一定的Web页面的程序或用于特定的URL。可选用下列几种:GET: 请求指定的页面信息,并返回实体主体。HEAD:...

115.HTTP请求之GET与POST方法比较

HTTP请求之GET与POST方法的区别和联系.
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)