分享自己写的Web Http请求内裤 C#

比较好用,试试就知道啦。用来发送http请求和获取回发数据

可以保存登录session。

 

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

namespace  MyWebRequest
{
    
/// <summary>
    
/// HttpWeb请求管理。
    
/// </summary>

    public class MyHttpWebRequest
    
{
        
/// <summary>
        
/// 保存网站Cookie。
        
/// </summary>

        private string m_cookieheader;
        
/// <summary>
        
/// 页面请求超时限制。
        
/// </summary>

        private const int TIMEOUT = 10000;
        

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


        
/// <summary>
        
/// 断开,清除Cookie。
        
/// </summary>

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


        
/// <summary>
        
/// 登录,记录会话。
        
/// </summary>

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


        
/// <summary>
        
/// Form提交方式获取响应数据。
        
/// </summary>

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


        
/// <summary>
        
/// Get方式获取响应数据。
        
/// </summary>

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


        
/// <summary>
        
/// Get方式获取响应数据。
        
/// </summary>

        public byte[] getPageData(String url, bool doSetCookie)
        
{
            
return getPageBytes(url, nullnull"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<paramList.Keys.Count; 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<paramstr.Length)
                        
{
                            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, 0256 );
                
while (count > 0
                
{
                    result.AddRange( GetSubBytes(read, 
0, count) );
                    count 
= sr.Read(read, 0256);
                }


            }
 
            
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();
        }

    }

}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值