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

  

C# WebHTTPUtil工具类的更多相关文章

  1. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  2. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  3. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  4. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  5. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

  6. Java程序员的日常—— Arrays工具类的使用

    这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...

  7. .net使用正则表达式校验、匹配字符工具类

    开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...

  8. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  9. JAVA 日期格式工具类DateUtil.java

    DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...

随机推荐

  1. 最常用且非常重要的Linux命令

    1.针对文件或目录类 cd: cat: ls: pwd: ln: mv: cp: vi.vim: find: mkdir: touch: echo: rm: chmod: chown: chattr: ...

  2. Linux入门-第九周

    1.判断UID是否大于等于500,如果为真就显示为普通用户,如果为假就显示为系统或管理用户 AWK简介:awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报 ...

  3. ATM-lib-common

    import logging.configfrom conf import settingsfrom core import src def get_logger(name): logging.con ...

  4. MySQL 如何生成日期表

    MySQL 如何生成日期表 在开发过程中,经常会遇到统计问题,通常交易信息都不是连续的,此时,统计出来的数据都是不连续的,所以提前生成一个时期表,当没有交易数据的时候填充0,就可以了,下面是生成日期表 ...

  5. RPC框架 - thrift 服务端

    -------服务端程序 ------ 下载    下载 thrift 源代码包    下载 thrift 的bin包 准备描述文件(使用源代码包的示例文件)    \thrift-0.10.0\tu ...

  6. oracle 11gR2 for win7旗舰版64安装以及连接plsql和NaviCat(win64_11gR2_database) (2012-12-31-bd 写的日志迁移

    先到oracle官网http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.html下载必要数 ...

  7. [译]The Python Tutorial#3. An Informal Introduction to Python

    3. An Informal Introduction to Python 在以下示例中,输入和输出以提示符(>>>和...)的出现和消失来标注:如果想要重现示例,提示符出现时,必须 ...

  8. 数论:HDU1066-Last non-zero Digit in N!

    题目: Last non-zero Digit in N! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  9. 1180: [CROATIAN2009]OTOCI(LCT)

    1180: [CROATIAN2009]OTOCI Time Limit: 50 Sec  Memory Limit: 162 MBSubmit: 1200  Solved: 747[Submit][ ...

  10. itchat 总结(转)

    python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...