当前位置:首页 > 后端开发 > C#模拟http 发送post或get请求

C#模拟http 发送post或get请求

6个月前 (05-23)44
private 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 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;
        }

  

在post的时候有时也用的到cookie,像登录163发邮件时候就需要发送cookie,所以在外部一个cookie属性随时保存 CookieContainer cookie = new CookieContainer();

!注意:有时候请求会重定向,但我们就需要从重定向url获取东西,像QQ登录成功后获取sid,但上面的会自动根据重定向地址跳转。我们可以用:
request.AllowAutoRedirect = false;设置重定向禁用,你就可以从headers的Location属性中获取重定向地址

作者:shineme
来源链接:https://www.cnblogs.com/xssxss/archive/2012/07/03/2574554.html

标签: HTTP

“C#模拟http 发送post或get请求” 的相关文章

ajax请求一直显示pending,IE显示挂起问题解决/http请求一直pending状态

ajax请求一直显示pending,IE显示挂起问题解决/http请求一直pending状态

前台使用ajax调用后台的一个接口,谷歌浏览器发现一直是pending状态,不超时,不报错。有的代码调用正常。 1.怀疑接口问题,但其他地方使用该接口正...

Postman—Http请求模拟工具

Postman—Http请求模拟工具

最近部门让我测试一个短信平台的接口,然后网上搜了一 搜,刚好找到了这个接口 一、Postman说明   Postman是一种网页调试与...

Qt发送HTTP请求

mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow>...

使用谷歌浏览器模拟发送http请求

使用谷歌浏览器模拟发送http请求

下载一个chromed的插件postman附上下载地址http://download.csdn.net/detail/zhenghui89/8490331;下载以后解压缩;打...

GO 发起HTTP请求调用接口

Go发起GET请求 基本的GET请求 //基本的GET请求 package main import ( "fmt" "io/ioutil"...

http请求中get请求可以缓存和post请求不可缓存

http请求中get请求可以缓存和post请求不可缓存

˃˃ _ Java侠"˃2019独角兽企业重金招聘Python工程师标准>>> GET请求后退/刷新无害,POS...

Http请求获取Token(HttpClient)

    在对接多个平台的项目时,有时候可能需要http先发送请求,获取token来保证,数据传输的安全性,尔后再进行对应的业务请求。 下面提供一种示...

pandownload http请求错误0x1c

pandownload http请求错误0x1c

pandownload 运行错误: 将IPV4的DHCP取消自动获取,使用“114.114.114.114”就可以正常使用了...

Android http网络请求设置以及设置网络权限

Android http网络请求设置以及设置网络权限

在project下 (一)HTTP网络请求设置: 第一步: 在res的xml目录下,新建一个xml文件,名称:network_securit...

HTTP的八种请求方法你知道吗?

HTTP的八种请求方法你知道吗?

文章目录 HTTP的请求方法 GET HEAD P...