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. 【前端_js】ajax的应用

    1.设置请求头部 function makeRequest() { alert("inside makeRequest()"); var settings = { type: &q ...

  2. oracle一些常用的数据类型

    字符数据类型 char数据类型 当需要固定长度时,使用char数据类型,此数据类型长度可以使1-2000字节.若是不指定大小默认占1字节,如果长度有空余时会以空格进行填充,如果大于设定长度 数据库则会 ...

  3. UVALive 4685 Succession 树DP+背包

    一.前言 这道题同样来自于红书P142,作为树DP专题中的一道比较难的题目,A了一天左右的时间,看上去事实证明,这题的难度理我本身的实力还是有些太远了,于是正确的做法应该是分析一下题目之后进行解析什么 ...

  4. 拓扑排序+不是字典序的优先级排列(POJ3687+HDU4857)

    一.前言 在过去的一周里结束了CCSP的比赛,其中有一道题卡了我9个小时,各种调错都没法完整的调处来这题,于是痛下决心开始补题,这个是计划的一部分.事实上,基于错误的理解我写了若干发拓扑排序+字典序的 ...

  5. 读取手机联系人,并用listview显示

    读取手机联系人,用到的就是一个contentprovider. 数据库里面有三张重要的表 raw_contact 里面有所有联系人的数据 data 每个联系人的所有数据 mime-type 每条数据的 ...

  6. 洛谷P1328生活大爆炸版石头剪刀布

    题目链接:https://www.luogu.org/problemnew/show/P1328

  7. 1 - smart(Maven:Package,Install,&,Log4j2)

    mvn package 时,增加如下命令-Dmaven.test.skip=true 则表示package打包时,不执行也不编译测试用例,mvn package -Dmaven.test.skip=t ...

  8. Fragment 和 Activity 之间通信

    在 Activity 中获取 Fragment 实例: FragmentManager 提供了一个类似于 findViewById 的方法,专门用于从布局文件中获取 Fragment 实例: //通过 ...

  9. Docker danriti/nginx-gunicorn-flask 使用

    现成的镜像,已经配置好nginx-gunicorn-flask,可直接部署flask 项目 直接部署flask项目 安装镜像 如果默认源比较慢,可以换成163镜像源 http://hub-mirror ...

  10. Chrome autocomplete="off"无效

    如果不希望输入框自动填充,可以设置 input 或 textarea 标签的属性 autocomplete="off". 但是有时 Chrome 会忽视 autocomplete ...