阅读更多
需要用到HttpWebResponse的开发,并且读取Stream的数据,贡献下我的源代码。例子虽然很简单,但是很实用。
3GEYE|u|IW*^ V
$J |p lJ)hi3 using System;
3GEYEGXn2o1h3WD
using System.Collections.Generic;
|e)_!B.Y*g3 using System.Text;
vvwnb;DB!W6E*l3
3GEYEq dcV-K!G M
using System.Net;
k-U"~ nDrK L A[5^v3 using System.IO;
A#Ji2B@*v3
$ws$I s4E%D6?3 namespace APIDemo
3GEYEd!k"| U*Dg)N4I
{
3GEYE&^q�?2s�~
class Program
j T9S;IP'q0c8bN3 {
3GEYE0r G!k0I"jO;@
static void Main(string[] args)
3GEYEEPC%w4Bi,b6k$\*k
{
3GEYED~Rl\qGx I
Console.WriteLine("Hello C#");
3GEYE7~mhc`o}b?
5I4b,[@&w j3 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://wap.uc123.com");
3GEYE'P8y;m7az;by\av9y
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
3GEYE%|g;@^b-C.a
3GEYEF�DsP"G8i,u1n }6k
Stream stream = httpWebResponse.GetResponseStream();
3GEYEjv8~vH hD/\b#u
byte[] bytes = new byte[1024];
3GEYE9Q.nU A,y
Bu8]o)Ay g]uh,J3 int numBytesToRead = (int)bytes.Length;
3GEYEn*L+r0p"Aoq
int numBytesRead = 0;
3GEYE0w{$Lt?@:{-y0r$V
//Console.WriteLine(stream.Length);
3GEYE:Y6K.]6[Ue(X
#c:C'}ZJL3 byte[] bs = Read2Buffer(stream,512);
6c,H"I.Z'S?+d&X B3
3GEYEO.Of[?$IeK5F3L
ioQ�h!?s3
X,v/f I%l e[2g$sL:yI3
3GEYES-p�T;n U/D.la:F
String text = System.Text.Encoding.UTF8.GetString(bs);
3GEYE Q1t)`sr
Console.WriteLine(text);
I5u0uoF3
3GEYE]il9U:h4Dc#b!Y
XHV*WS+C'YW g3 Console.Read();
/d#z!B�da3
3GEYExN%]#}E`e
@-p8P] aZ beF3xt3 //Stream
file = httpWebResponse.GetResponseStream();
3GEYEp ~(@BJL t0j
[Z E!s5W$}2S*K;c]$N3
\2NO!Pw){x g#h-h'N3 }
3GEYE[_u Ot
3GEYE'i6I1he"x/S:n
public static byte[] Read2Buffer(Stream stream, int BufferLen)
3GEYE!?/}B;~["q |j7`J
{
3GEYE.O9?2N:jEQk2|
3GEYE9oJQXp)P?l-q
// 如果指定的无效长度的缓冲区,则指定一个默认的长度作为缓存大小
3GEYE,Yp4N+X2d`)F xdO
3GEYE+eLb|JWx%e#j
if (BufferLen < 1)
b7mx@r4A |r T3 {
3P5A5_1@xvr3
3GEYE*Bv s�b?
BufferLen = 0x8000;
$o1t~D/L0m3
5Dr#c:Y{�GIeO3 }
5DHF/g;y3pm3
3GEYEza}�a!nn:s}9?Nh
// 初始化一个缓存区
hx XS)qMn J5a3
3GEYE3Y7{_yG
byte[] buffer = new byte[BufferLen];
jVxo{O3
nU%}moN"O2F3 int read = 0;
9\\_mW$?/p/I3
3GEYE1Q,wgV*k}!AE9c
int block;
3GEYE!m&oU3B%JjY
CoA6hl$x[A3 // 每次从流中读取缓存大小的数据,知道读取完所有的流为止
"\0m3HThJ3
3GEYE'ehD"k;nmSYL.a
while ((block = stream.Read(buffer, read, buffer.Length - read)) > 0)
3GEYE+wW.QXO
{
3GEYE~8Y ? p L
s5@P Z,C%p1DF,`3 // 重新设定读取位置
3GEYE8ZbT ~4rp"~
*LO)X8X`X\moq3 read += block;
*S(bc:H�Dg3
|fWB:p4Xi w.H.b3
3GEYEtt,p)K`R5z6Z
j_h8zb�Yq3 // 检查是否到达了缓存的边界,检查是否还有可以读取的信息
^2m]:F#m0W'gi']:i'yY3
\,R*xiBk$P3 if (read == buffer.Length)
3GEYE?#gv{+c8q-N
{
2H�yN;G'u\3
3GEYEk!Am/Dl:a0r:P4c I
// 尝试读取一个字节
ecE:whv�aK/u2s3
3GEYE,v5z1j!jot
int nextByte = stream.ReadByte();
?&\Lw/B7W!W7m-J3
,z f]'fO#_3
\#Qivv u9P4kP3
!m`!W0T|7sU2jI3 // 读取失败则说明读取完成可以返回结果
H'^5_B UuS3
3GEYEP$d\{md�@(A
if (nextByte == -1)
@}n&eQ"evO3 {
GU7E%Q&BB3
]6n4?;|#{-l3 return buffer;
3GEYE Gi W!~X1zv6xD-k
A~ DCO\8qA:T3 }
3GEYE?&CZ^%~vPI @I
3GEYE y `ph%bN/dBS6r
3GEYEhJA~+nH
8oa}0Az+K \`�e3 // 调整数组大小准备继续读取
O x(`#~W8r,i3
l Uqa�k_.d3 byte[] newBuf = new byte[buffer.Length * 2];
3GEYE8sG9MGI1qA'k
3GEYE$j0um[2P8{VUw
Array.Copy(buffer, newBuf, buffer.Length);
Mc U4Qn8lj!GF3
&@ Od;xmP7s3 newBuf[read] = (byte)nextByte;
0x+^`.tP D3
x1m%O-Q1I(o1u3 buffer = newBuf;// buffer是一个引用(指针),这里意在重新设定buffer指针指向一个更大的内存
Lc4YU8z&l z3
i/j,[(Q}.lw3 read++;
7I0UJ'P9ys0t3
3GEYEJ:N3W6WZ5xi&z