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;
} }
C# WebHTTPUtil工具类的更多相关文章
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
- Java程序员的日常—— Arrays工具类的使用
这个类在日常的开发中,还是非常常用的.今天就总结一下Arrays工具类的常用方法.最常用的就是asList,sort,toStream,equals,copyOf了.另外可以深入学习下Arrays的排 ...
- .net使用正则表达式校验、匹配字符工具类
开发程序离不开数据的校验,这里整理了一些数据的校验.匹配的方法: /// <summary> /// 字符(串)验证.匹配工具类 /// </summary> public c ...
- WebUtils-网络请求工具类
网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...
- JAVA 日期格式工具类DateUtil.java
DateUtil.java package pers.kangxu.datautils.utils; import java.text.SimpleDateFormat; import java.ut ...
随机推荐
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- python 实现无序列表
# -*- coding:utf-8 -*- class Node: def __init__(self, initdata): self.data = initdata self.next = No ...
- Cube HDU - 1220(思维)
Cowl is good at solving math problems. One day a friend asked him such a question: You are given a c ...
- ACM-ICPC 2018 徐州赛区网络预赛
A. Hard to prepare #include <bits/stdc++.h> using namespace std; ; ]; ]; int main() { int T; i ...
- Hive UDF开发指南
编写Apache Hive用户自定义函数(UDF)有两个不同的接口,一个非常简单,另一个...就相对复杂点. 如果你的函数读和返回都是基础数据类型(Hadoop&Hive 基本writable ...
- 使用Spark Streaming + Kudu + Impala构建一个预测引擎
随着用户使用天数的增加,不管你的业务是扩大还是缩减了,为什么你的大数据中心架构保持线性增长的趋势?很明显需要一个稳定的基本架构来保障你的业务线.当你的客户处在休眠期,或者你的业务处在淡季,你增加的计算 ...
- Nodejs-模块化结构
1.模块(一个文件就是一个模块) 获取当前脚本所在的路径 _ _dirname 文件路径 _ _filename (1)创建模块(module1.js) const fs=require('fs'); ...
- vue-cli 中引入 jq
vue-cli webpack 引入jquery 今天费了一下午的劲,终于在vue-cli 生成的工程中引入了jquery,记录一下.(模板用的webpack) 首先在package.json里的 ...
- cf982d Shark
ref #include <algorithm> #include <iostream> #include <cstdio> #include <map> ...
- C#入门篇-3:数据类型及转换
using System; using System.Text; using System.Collections; using System.Collections.Generic; //003 查 ...