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 ...
随机推荐
- 20145236 《Java程序设计》第九周学习总结
20145236 <Java程序设计>第九周学习总结 教材学习内容总结 第十六章 整合数据库 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API ...
- Linux下lzop命令安装
[root@xd502djj ~]# yum install lzop Loaded plugins: fastestmirror Determining fastest mirrors * base ...
- 数据库索引<二> 补充前篇
你要准备的软件有: 最新版 Rsync for windows 服务端:cwRsync_Server_2.1.5_Installer.zip 客户端:cwRsync_2.1.5_Installer.z ...
- Bag of mice(CodeForces 148D )
D. Bag of mice time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 454. 4Sum II ——查找本质:hash最快,二分次之
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...
- boost 源码编译 的 Makefile.am写法备份
include $(top_srcdir)/common.mk bin_PROGRAMS= lib_LIBRARIES= lib_LTLIBRARIES= lib_LTLIBRARIES+=libSt ...
- office 使用技巧
Excel: 非打印区域显示成灰色:视图--分页预览 选定行的时侯,如何从某一行选定到末尾? 先点某一行,然后按住shift不松,再按END,再按下方向箭. 选定列也是这样,先按住某列,然后按SHIF ...
- Swift - 自动布局库SnapKit的使用详解2(约束的更新、移除、重做)
在之前的文章中我介绍了如何使用SnapKit的 snp_makeConstraints 方法进行各种约束的设置.但有时我们的页面并不是一直固定不变的,这就需要修改已经存在的约束.本文介绍如何更新.移除 ...
- java之io之file类的常用操作
java io 中,file类是必须掌握的.它的常用api用法见实例. package com.westward.io; import java.io.File; import java.io.IOE ...
- 10 notorious computer virus
The history of computer virus is the same as computer history. With more and more powerful computers ...