C#写的Web Http请求

来自:http://blog.csdn.net/ccccccccccc/archive/2007/10/25/1842629.aspx

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Text;
using System.Collections.Specialized;

namespace MyWebRequest
...{
/**


/// HttpWeb请求管理。
///

public class MyHttpWebRequest
...{
/**
/// 保存网站Cookie。
///

private string m_cookieheader;
/**
/// 页面请求超时限制。
///

private const int TIMEOUT = 10000;


public MyHttpWebRequest()
...{
//
// 构造函数逻辑
//
this.m_cookieheader = "";
        }

/**
/// 断开,清除Cookie。
///

public void Disconnect()
...{
this.m_cookieheader = "";
        }

/**
/// 登录,记录会话。
///

public string Login(String url, NameValueCollection paramList, System.Text.Encoding wideCharEncoding)
...{
return getPage(url, paramList, wideCharEncoding, "POST", true);
        }

/**
/// Form提交方式获取响应数据。
///

public string post(String url, NameValueCollection paramList, System.Text.Encoding wideCharEncoding) 
...{
return getPage(url, paramList, wideCharEncoding, "POST", false);
        }

/**
/// Get方式获取响应数据。
///

public string getPage(String url) 
...{
return getPage(url, null, null, "GET", false);
        }

/**
/// Get方式获取响应数据。
///

public byte[] getPageData(String url, bool doSetCookie)
...{
return getPageBytes(url, null, null, "GET", doSetCookie);
        }

private string getPage(String url, NameValueCollection paramList, System.Text.Encoding wideCharEncoding, string method, bool doSetCookie) 
...{
return System.Text.Encoding.Default.GetString( getPageBytes(url, paramList, wideCharEncoding, method, doSetCookie) );
        }

private byte[] getPageBytes(String url, NameValueCollection paramList, System.Text.Encoding wideCharEncoding, string method, bool resetCookie) 
...{
            HttpWebResponse res = null;
            System.Collections.ArrayList result = new System.Collections.ArrayList(5000);

try
...{
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = method;
                req.KeepAlive = true;
                req.ContentType = "application/x-www-form-urlencoded";
                req.CookieContainer = new CookieContainer();
                req.CookieContainer.SetCookies(new Uri(url), this.m_cookieheader);
                req.Timeout = TIMEOUT;


//上行方式时,设置参数
if(method.ToUpper() != "GET")
...{
                    StringBuilder UrlEncoded = new StringBuilder();
                    Char[] reserved = ...{'?', '=', '&'};
byte[] SomeBytes = null;

if (paramList != null) 
...{
                        System.Text.StringBuilder paramBuilder = new System.Text.StringBuilder();
                        String paramstr = null;
for(int li=0; li
...{
if(li > 0)
...{
                                    paramBuilder.Append("&");
                            }
                            paramBuilder.Append(paramList.Keys[li]);
                            paramBuilder.Append("=");
                            paramBuilder.Append(paramList[paramList.Keys[li]]);
                        }

                        paramstr = paramBuilder.ToString();

int i=0, j;
while(i
...{
                            j=paramstr.IndexOfAny(reserved, i);
if (j==-1)
...{
                                UrlEncoded.Append(HttpUtility.UrlEncode(paramstr.Substring(i, paramstr.Length-i), wideCharEncoding ));
break;
                            }
                            UrlEncoded.Append(HttpUtility.UrlEncode(paramstr.Substring(i, j-i), wideCharEncoding));
                            UrlEncoded.Append(paramstr.Substring(j,1));
                            i = j+1;
                        }
                        SomeBytes = Encoding.ASCII.GetBytes(UrlEncoded.ToString());

                        req.ContentLength = SomeBytes.Length;
                        Stream newStream = req.GetRequestStream();
                        newStream.Write(SomeBytes, 0, SomeBytes.Length);
                        newStream.Close();
                    }
else
...{
                        req.ContentLength = 0;
                    }
                }


//请求响应
                res = (HttpWebResponse)req.GetResponse();
if(resetCookie)
...{
this.m_cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
                }

//读取数据
                Stream ReceiveStream = res.GetResponseStream();
                System.IO.BinaryReader sr = new BinaryReader(ReceiveStream);
byte[] read = new byte[256];
int count = sr.Read( read, 0, 256 );
while (count > 0) 
...{
                    result.AddRange( GetSubBytes(read, 0, count) );
                    count = sr.Read(read, 0, 256);
                }

            }
catch
...{
                result.Clear();
            }
finally
...{
if ( res != null ) 
...{
                    res.Close();
                }
            }

return (byte[])result.ToArray(typeof(byte));
        }

private byte[] GetSubBytes(byte [] buffer, int index, int count)
...{
if ( (count+index)>buffer.Length ) 
...{
                count = buffer.Length-index;
            }
            System.IO.MemoryStream s = new System.IO.MemoryStream(buffer, index, count);
return s.ToArray();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值