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. 一次磁盘IO过高分析过程

    1.查看监控,发现整点时间有写IO过高情况

  2. MySQL主从复制原理及配置过程

    一.Mysql数据库的主从复制原理过程: (多实例的安装请参考我的另一篇文章:https://www.cnblogs.com/Template/p/9258500.html) Mysql的主从复制是一 ...

  3. 常用的几个JQuery代码片段

    1. 导航菜单背景切换效果 在项目的前端页面里,相对于其它的导航菜单,激活的导航菜单需要设置不同的背景.这种效果实现的方式有很多种,下面是使用JQuery实现的一种方式: //注意:代码需要修饰完善 ...

  4. tp5.1发送邮件

    <?php namespace app\admin\controller; use think\Controller; use think\Request; use PHPMailer\PHPM ...

  5. html5定位获取当前位置并在百度地图上显示

    用html5的地理定位功能通过手机定位获取当前位置并在地图上居中显示出来,下面是百度地图API的使用过程,有需要的朋友可以参考下 在开发移动端 web 或者webapp时,使用百度地图 API 的过程 ...

  6. python3+openCV实现图片的人脸人眼检测,原理+参数+源代码

    上学时候用matlab学过一些图像处理的基础知识,当时课程作业是用haar实现人脸检测 but当时是心思根本不在图像处理上,so找了个同学帮忙做的,自己没上心 然鹅天道好轮回,现在捡起来了原来的算法一 ...

  7. 水题:HDU-1088-Write a simple HTML Browser(模拟题)

    解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...

  8. 洛谷P2389 电脑班的裁员(区间DP)

    题目背景 隔壁的新初一电脑班刚考过一场试,又到了BlingBling的裁员时间,老师把这项工作交给了ZZY来进行.而ZZY最近忙着刷题,就把这重要的任务交(tui)给了你. 题目描述 ZZY有独特的裁 ...

  9. Android stadio 调试太掉了

    1.evalute expresstion 可以看任何变量的任何属性,就算是一个字符串url,你可以url.length(),你不用输入完就有提示.对象的方法有提示! 2.调试技巧 就是当一行里面有很 ...

  10. Redis实现之对象(一)

    对象 前面我们介绍了Redis的主要数据结构,如:简单动态字符串SDS.双端链表.字典.压缩列表.整数集合等.Redis并没有直接使用这些数据结构来实现键值对数据库,而是基于这些数据结构创建了一个对象 ...