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;
        }
版权声明:本文为博主原创文章,未经博主允许不得转载。

模拟GET/POST方法发送HTTP请求

  • 2006年01月13日 09:51
  • 447KB
  • 下载

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

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

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

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

如何调用http接口通过get()/post()方法传参

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; impo...
  • CNZYYH
  • CNZYYH
  • 2017年06月22日 14:06
  • 106
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HTTP请求接口方法(POST/GET)
举报原因:
原因补充:

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