2008-04-07
开发我的.Net 简单的网络程序。
需要用到HttpWebResponse的开发,并且读取Stream的数据,贡献下我的源代码。例子虽然很简单,但是很实用。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace APIDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello C#");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://wap.uc123.com");
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebResponse.GetResponseStream();
byte[] bytes = new byte[1024];
int numBytesToRead = (int)bytes.Length;
int numBytesRead = 0;
//Console.WriteLine(stream.Length);
byte[] bs = Read2Buffer(stream,512);
String text = System.Text.Encoding.UTF8.GetString(bs);
Console.WriteLine(text);
Console.Read();
//Stream file = httpWebResponse.GetResponseStream();
}
public static byte[] Read2Buffer(Stream stream, int BufferLen)
{
// 如果指定的无效长度的缓冲区,则指定一个默认的长度作为缓存大小
if (BufferLen < 1)
{
BufferLen = 0x8000;
}
// 初始化一个缓存区
byte[] buffer = new byte[BufferLen];
int read = 0;
int block;
// 每次从流中读取缓存大小的数据,知道读取完所有的流为止
while ((block = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
// 重新设定读取位置
read += block;
// 检查是否到达了缓存的边界,检查是否还有可以读取的信息
if (read == buffer.Length)
{
// 尝试读取一个字节
int nextByte = stream.ReadByte();
// 读取失败则说明读取完成可以返回结果
if (nextByte == -1)
{
return buffer;
}
// 调整数组大小准备继续读取
byte[] newBuf = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuf, buffer.Length);
newBuf[read] = (byte)nextByte;
buffer = newBuf;// buffer是一个引用(指针),这里意在重新设定buffer指针指向一个更大的内存
read++;
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace APIDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello C#");
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://wap.uc123.com");
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebResponse.GetResponseStream();
byte[] bytes = new byte[1024];
int numBytesToRead = (int)bytes.Length;
int numBytesRead = 0;
//Console.WriteLine(stream.Length);
byte[] bs = Read2Buffer(stream,512);
String text = System.Text.Encoding.UTF8.GetString(bs);
Console.WriteLine(text);
Console.Read();
//Stream file = httpWebResponse.GetResponseStream();
}
public static byte[] Read2Buffer(Stream stream, int BufferLen)
{
// 如果指定的无效长度的缓冲区,则指定一个默认的长度作为缓存大小
if (BufferLen < 1)
{
BufferLen = 0x8000;
}
// 初始化一个缓存区
byte[] buffer = new byte[BufferLen];
int read = 0;
int block;
// 每次从流中读取缓存大小的数据,知道读取完所有的流为止
while ((block = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
// 重新设定读取位置
read += block;
// 检查是否到达了缓存的边界,检查是否还有可以读取的信息
if (read == buffer.Length)
{
// 尝试读取一个字节
int nextByte = stream.ReadByte();
// 读取失败则说明读取完成可以返回结果
if (nextByte == -1)
{
return buffer;
}
// 调整数组大小准备继续读取
byte[] newBuf = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuf, buffer.Length);
newBuf[read] = (byte)nextByte;
buffer = newBuf;// buffer是一个引用(指针),这里意在重新设定buffer指针指向一个更大的内存
read++;
评论
jianglinchun
2008-04-09
不要忘记实现了IDisposeable接口的还有using方法
wuhua
2008-04-09
?????
tmj
2008-04-09
怎么会被推荐?
发表评论
- 浏览: 562647 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
我女朋友
共 12 张
共 12 张
最新评论
-
Google产品大全
很全很全很全很全
-- by sheandwei -
说真的UCWEB看奥运真方便
楼下的。话不能这么说。我现在上班开奥运就靠它了,
-- by wuhua -
说真的UCWEB看奥运真方便
傻佬,这也值得说
-- by caohj -
UCWEB使用秘诀
最近发现UCWEB有准备发布新版本了。到时再仔细研究相关的一些特殊用法。
-- by wuhua -
开始进入另外一个领域
自己的水平还很菜啊。要努力学习
-- by wuhua






评论排行榜