public static string Post(string Url, string postDataStr, string cookies)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST";
if (cookies != null)
request.Headers.Add("Cookie", cookies);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
writer.Write(postDataStr);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
} public static string Get(string Url, string cookies)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "GET";
if (cookies != null)
request.Headers.Add("Cookie", cookies);
request.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
string retString = reader.ReadToEnd();
return retString;
}

C# HttpRequest 请求的更多相关文章

  1. django中HttpRequest请求

    视图的第一个参数必须是HttpRequest对象 在视图函数中,接收的request有如下属性: path:一个字符串,表示请求的页面的完整路径,不包含域名. method:一个字符串,表示请求使用的 ...

  2. Webservice 或者HttpRequest请求的时候提示 “指定的注册表项不存在”错误 解决方案

    今天又遇到神奇的事情,在使用WebService的时候居然提示“指定的注册表不存在.” The specified registry key does not exist. Google后发现,原来是 ...

  3. 通过HttpWebRequest请求https接口

    一.为什么进行代理接口的开发: 有些项目需要访问被墙了哒网站,比如前不久公司开发项目需要使用google地图的接口,而google在中国被墙了,所有打算做一个代理接口服务,将代理放到国外服务器上,通过 ...

  4. Android 学习笔记之AndBase框架学习(三) 使用封装好的函数完成Http请求..

    PS:踏踏实实走好每一步... 学习内容: 1.使用AndBase框架实现无参Http Get请求... 2.使用AndBase框架实现有参Http Post请求... 3.使用AndBase框架实现 ...

  5. httpclient发送request请求时设置header和timeout

    package com.xxx.xxx.common; import java.io.BufferedReader; import java.io.InputStreamReader; import ...

  6. 4.1ASP.NET Core请求过程「深入浅出ASP.NET Core系列」

    希望给你3-5分钟的碎片化学习,可能是坐地铁.等公交,积少成多,水滴石穿,谢谢关注. HTTP请求过程 这里展示整体的HTTP请求的过程,这里化繁为简,保留了主干流程: 从浏览器输入域名开始,这里忽略 ...

  7. IOS网络篇1之截取本地URL请求(NSURLProtocol)

    本文转载至 http://blog.csdn.net/u014011807/article/details/39894247 NSURLProtocol 是iOS中非常重要的一个部分,我们经常会在以下 ...

  8. 接口自动化·分享·第二篇·你必须了解的HttpRequest和HttpResponse

    完成一个接口调用其实就是完成了一次http请求,所以你必须要清楚一个http请求的组成. 一次完整的请求包含:请求+响应. 一.HttpRequest请求对象 要调用一个接口,首先要准备的是一个请求对 ...

  9. 白鹭http请求post

    示例demo: //new http请求 var request = new egret.HttpRequest(); //请求参数 var params = "p1=postP1& ...

随机推荐

  1. Redis 源码简洁剖析 05 - ziplist 压缩列表

    ziplist 是什么 Redis 哪些数据结构使用了 ziplist? ziplist 特点 优点 缺点 ziplist 数据结构 ziplist 节点 pre_entry_length encod ...

  2. SpringBoot前后端数组交互

    前端 后端 Gitee地址 https://gitee.com/zhuayng/foundation-study.git 参考 https://blog.csdn.net/qq_34091758/ar ...

  3. 在java中静态方法与非静态方法

    在java中public void与public static void有什么区别 ? public void 修饰是非静态方法,该类方法属于对象,在对象初始化(new Object())后才能被调用 ...

  4. Docker入门 安装 基础操作命令

    Docker 学习来源 https://www.bilibili.com/video/av26993050/?spm_id_from=333.788.b_636f6d6d656e74.20 https ...

  5. 用Java开发局域网内文件传输软件遇到的一些问题

    项目地址:https://github.com/b84955189/FileTransfer 由于巨懒的我不太喜欢使用U盘操作文件,特此开发一个简易的文件传输程序. 目前仅限局域网内传输,后期会尝试写 ...

  6. 自定义的类实现copy操作

    1.自定义类实现copy操作 让类遵守NSCopying协议 实现 copyWithZone:方法,在该方法中返回一个对象的副本即可. 在copyWithZone方法中,创建一个新的对象,并设置该对象 ...

  7. php常考功能函数

    1.统计数组元素个数 $arr = array( '1011,1003,1008,1001,1000,1004,1012', '1009', '1011,1003,1111' ); $result = ...

  8. 实例15_C语言绘制万年历

    实例说明:

  9. 关于final关键字

    final修饰基本数据类型时 修饰的变量值不可变 final修饰引用数据类型时 修饰的变量地址不可变 值可变 final修饰一个类中的方法时 不可被子类重写 final修饰一个类时 不可被其他类继承 ...

  10. VUE动态生成table表格(element-ui)(新增/删除)

    (直接复制即可测试) 结构(红色部分 data/prop/v-model 数据绑定): <template> <el-table size="small" :da ...