• C# WebHTTPUtil工具类


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    public class WebHTTPUtil
    {
    	public CookieContainer CookieContainer { get; set; }
    	public CookieCollection CookieCollection { get; set; }
    	public WebRequest Request{
    		get; set;
    	}
    
    	public WebHTTPUtil()
    	{
    		this.CookieCollection = new CookieCollection();
    		this.CookieContainer = new CookieContainer();
    	}
    
    	/// <summary>
    	/// 以POST 形式请求数据
    	/// </summary>
    	/// <param name="RequestPara"></param>
    	/// <param name="Url"></param>
    	/// <returns></returns>
    	public  string PostData(string Url,string RequestPara)
    	{
    		System.GC.Collect ();
    		Request = HttpWebRequest.Create(Url);
    		RequestPara=Regex.Replace(RequestPara,"%", "%25");
    		byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);
    		Request.ContentType = "application/x-www-form-urlencoded";
    		Request.ContentLength = buf.Length;
    		Request.Method = "POST";
    		
    		System.IO.Stream RequestStream = Request.GetRequestStream();
    		RequestStream.Write(buf, 0, buf.Length);
    		RequestStream.Close();
    		
    		System.Net.WebResponse response = Request.GetResponse();
    		StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
    		string ReturnVal = reader.ReadToEnd();
    		reader.Close();            
    		response.Close();
    		Request = null;
    		return ReturnVal;
    	}
    	
    	/// <summary>
    	/// 以GET 形式获取数据
    	/// </summary>
    	/// <param name="RequestPara"></param>
    	/// <param name="Url"></param>
    	/// <returns></returns>
    	
    	public  string GetData(string Url, string RequestPara)
    	{
    		System.GC.Collect ();
    		RequestPara=RequestPara.IndexOf('?')>-1?(RequestPara):("?"+RequestPara);
    		RequestPara=Regex.Replace(RequestPara,"%", "%25");
    		Request = HttpWebRequest.Create(Url + RequestPara);
    		
    		byte[] buf = System.Text.Encoding.GetEncoding("utf-8").GetBytes(RequestPara);          
    		Request.Method = "GET";
    		
    		System.Net.WebResponse response = Request.GetResponse();
    		string status = ((HttpWebResponse)response).StatusDescription;
    		if (!status.Equals ("OK")) {
    			response.Close();
    			return "ERROR";
    		}
    		StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8"));
    
    		string ReturnVal = reader.ReadToEnd();
    		reader.Close();
    		response.Close();
    		Request = null;
    		return ReturnVal;
    	}
    	
    }
    

      

  • 相关阅读:
    java.util.Arrays类详解
    爬虫
    学习Spring框架(一)
    JAVA的网络编程
    Thread直接调用run()和start()方法的区别
    JDK JRE JVM 区别
    TCP , HTTP, IP
    一些常用的端口
    PATH CLASSTH JAVA_HOME
    浅谈Linux内存管理机制
  • 原文地址:https://www.cnblogs.com/ylhssn/p/6015915.html
Copyright © 2020-2023  润新知