HttpWebRequest 和HttpWebResponse总结
1. 总结

总结2

3. Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Web; namespace Core
{
public class RequestHelper
{
private Stream SendGetRequest1(string url)
{
string content;
//HttpRequest request;// Enables ASP.NET to read the HTTP values sent by a client during a Web request.
WebRequest webrequest;// Makes a request to a Uniform Resource Identifier (URI). This is an abstract
HttpWebRequest httpWebRequest;// Provides an HTTP-specific implementation of the System.Net.WebRequest class.
HttpWebResponse httpWebResponse;
//string url = "fsefsf";
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Method = "GET";
httpWebRequest.ContentType = "application/x-www-form-urlencoded"; //httpwebrequest.ContentType = "application/json";
//httpwebrequest.ContentType = "application/xml";
//httpwebrequest.Headers.Add("url",url); httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream resStream = httpWebResponse.GetResponseStream();
using (StreamReader sr = new StreamReader(resStream))
{
content = sr.ReadToEnd();
}
if (httpWebResponse.StatusCode != HttpStatusCode.OK)
{ }
return resStream;
}
private Stream SendGetRequestForStream(string url, string contentType)
{
HttpWebRequest httpWebRequest;// Provides an HTTP-specific implementation of the System.Net.WebRequest class.
HttpWebResponse httpWebResponse;
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "GET";
httpWebRequest.ContentType = contentType;
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream resStream = httpWebResponse.GetResponseStream();
return resStream;
} private String SendGetRequest(string url, string contentType)
{
string content = "";
HttpWebRequest httpWebRequest;
HttpWebResponse httpWebResponse;
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "GET";
httpWebRequest.ContentType = contentType;//"application/x-www-form-encoded";
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream resStream = httpWebResponse.GetResponseStream();
using (StreamReader sr = new StreamReader(resStream))
{
content = sr.ReadToEnd();
}
return content;
} private Stream SendPostRequestForStream(string url, string data, string contentType)
{
//string content = "";
HttpWebRequest httpWebRequest;
HttpWebResponse httpWebResponse;
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
Stream reqStream = httpWebRequest.GetRequestStream();
using (StreamWriter sw = new StreamWriter(reqStream))
{
sw.Write(data);
}
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream ResStream = httpWebResponse.GetResponseStream(); return ResStream;
} private String SendPostRequest(string url, string data, string contentType)
{
string content = "";
HttpWebRequest httpWebRequest;
HttpWebResponse httpWebResponse;
httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
Stream reqStream = httpWebRequest.GetRequestStream();
using (StreamWriter sw = new StreamWriter(reqStream))
{
sw.Write(data);
}
httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
Stream resStream = httpWebResponse.GetResponseStream();
using (StreamReader sr = new StreamReader(resStream))
{
content = sr.ReadToEnd();
} return content;
}
}
}
HttpWebRequest 和HttpWebResponse总结的更多相关文章
- C# HttpWebRequest与HttpWebResponse详解
C# HttpWebRequest与HttpWebResponse详解 http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...
- 使用HttpWebRequest以及HttpWebResponse读取Http远程文件
主页>杂项技术>.NET(C#)> 使用HttpWebRequest以及HttpWebResponse读取Http远程文件 jackyhwei 发布于 2010-08-15 21: ...
- HttpWebRequest和HttpWebResponse用法小结
http://www.cnblogs.com/willpan/archive/2011/09/26/2176475.html http://www.cnblogs.com/lip0121/p/4539 ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie
之前看过某个同学的一篇有关与使用JSoup解析学校图书馆的文章,仔细一看,发现竟然是同校!!既然对方用的是java,那么我也就来个C#好了,虽然我的入门语言是java. C#没有JSoup这样方便的东 ...
- c# HttpWebRequest与HttpWebResponse 绝技(转载)
c# HttpWebRequest与HttpWebResponse 绝技 如果你想做一些,抓取,或者是自动获取的功能,那么就跟我一起来学习一下Http请求吧.本文章会对Http请求时的Get和P ...
- C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse
上次介绍了用WebClient的方式提交POST请求,这次,我继续来介绍用其它一种方式 HttpWebRequest以及HttpWebResponse 自认为与上次介绍的WebClient最大的不同之 ...
- C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站
原文:C# 利用 HttpWebRequest 和 HttpWebResponse 模拟登录有验证码的网站 我们经常会碰到需要程序模拟登录一个网站,那如果网站需要填写验证码的要怎样模拟登录呢?这篇文章 ...
- 利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录
利用HttpWebRequest和HttpWebResponse获取Cookie并实现模拟登录 tring cookie = response.Headers.Get("Set-Cookie ...
- C# -- HttpWebRequest 和 HttpWebResponse 的使用
C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebRequest 和 HttpWebResponse,来判断一个网页地址是否可以正常访问. 1 ...
- C#使用HttpWebRequest和HttpWebResponse上传文件示例
C#使用HttpWebRequest和HttpWebResponse上传文件示例 1.HttpHelper类: 复制内容到剪贴板程序代码 using System;using System.Colle ...
随机推荐
- 超实用的JavaScript代码段 Item8 -- js对象的(深)拷贝
js 对象 浅拷贝 和 深拷贝 1.浅拷贝 拷贝就是把父对像的属性,全部拷贝给子对象. 下面这个函数,就是在做拷贝: var Chinese = { nation:'中国' } var Doctor ...
- 怎么实现form表单提交后不重新刷新当前页面
怎么实现表单提交后不重新刷新当前页面 如何实现表单提交后不重新刷新当前页面 <form name='form1' id='form1' action='/xbcw/cw/xx_xx.ac ...
- 生产订单修改删除组件BDC
可用函数修改:CO_XT_COMPONENT_CHANGE,一次一个 FORM prm_change_bom . DATA:gw_zstypf TYPE zstypf. DATA:lv_rspos T ...
- uva 1629
1629 - Cake slicing Time limit: 3.000 seconds A rectangular cake with a grid of m * n <tex2html_v ...
- matlab(数组、矩阵)
- 学习记录008-crond和visudo
1.每隔两个小时将/etc/servers文件打包备份到.tmp下(每次名字不同) [root@kaka cc.log]# tar zcvf /server/backup/ccc.log_$(date ...
- java 面向对象编程-- 第15章 集合框架
1. 集合特点:元素类型不同.集合长度可变.空间不固定 2. java中对一些数据结构和算法进行了封装即集合.集合也是一种对象,用于存储.检索.操作和传输对象. 3. JCF(Java Coll ...
- uva 10491
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 分析与提取QQ木马盗号技术
程序大致的流程如下图: 因为是用画图工具画的,所以大家就将就看下把,有什么不对的地方请多多指教: 程序是用Delphi写的,只有加载器加了个upx壳,其他的都没有加壳:所以分析起来就比较简单了: 这个 ...
- POJ 3009 Curling 2.0 回溯,dfs 难度:0
http://poj.org/problem?id=3009 如果目前起点紧挨着终点,可以直接向终点滚(终点不算障碍) #include <cstdio> #include <cst ...