C# HttpRequest
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using BDIC_BASE.Bonc.UI.Util; namespace BDIC_BASE.Bonc.DAL
{
public class HttpRequest
{
public static string DoPost(string url, Hashtable paramsOfUrl)
{
if (url == null)
{
throw new Exception("url 为空");
//return "";
}
// 编辑并Encoding提交的数据
byte[] data = GetJointBOfParams(paramsOfUrl); // 发送请求
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length; Stream stream = request.GetRequestStream();
stream.Write(data, , data.Length);
stream.Close(); // 获得回复
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close(); return result;
} public static string DoGet(string url, Hashtable paramsOfUrl)
{
if (url == null)
{
throw new Exception("url 为空");
//return "";
}
// 编辑并Encoding提交的数据
string data = GetJointSOfParams(paramsOfUrl);
// 拼接URL
url += "?" + data; // 发送请求
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
request.Method = "GET"; // 获得回复
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string result = reader.ReadToEnd();
reader.Close(); return result;
} private static String GetJointSOfParams(Hashtable paramsOfUrl)
{
// 编辑并Encoding提交的数据
StringBuilder sbuilder = new StringBuilder();
int i = ;
foreach (DictionaryEntry de in paramsOfUrl)
{
if (i == )
{
sbuilder.Append(de.Key + "=" + de.Value);
}
else
{
sbuilder.Append("&" + de.Key + "=" + de.Value);
}
}
return sbuilder.ToString();
} private static byte[] GetJointBOfParams(Hashtable paramsOfUrl)
{
// 编辑并Encoding提交的数据
String stringJointOfParams = GetJointSOfParams(paramsOfUrl);
byte[] data = new ASCIIEncoding().GetBytes(stringJointOfParams); return data;
}
} public class HttpParam
{
public HttpParam()
{
} private Hashtable _paramsOfUrl;
public Hashtable ParamsOfUrl
{
get
{
if (_paramsOfUrl == null)
{
_paramsOfUrl = Hashtable.Synchronized(new Hashtable());
}
return _paramsOfUrl;
}
set { _paramsOfUrl = value; }
} public void AddParamOfUrl(String paramKey ,String paramValue)
{
try
{
ParamsOfUrl.Add(paramKey, paramValue);
}
catch (Exception ex)
{
Console.WriteLine("可能为key重复\n详细:" + ex.Message);
}
}
}
}
C# HttpRequest的更多相关文章
- .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...
- 防刷票机制研究和.NET HttpRequest Proxy
最近应朋友之约 测试他做的投票网站 防刷票机制能力如何,下面有一些心得和体会. 朋友网站用PHP写的,走的是HttpRequest,他一开始认为IP认证应该就差不多了.但说实话这种很low,手动更换代 ...
- python httprequest, locust
r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...
- c# WebBrower 与 HttpRequest配合 抓取数据
今天研究一个功能,发现一个问题. 通过webbrower模拟用户自动登录可以完成,并且可以取到相对应的页面内容. 但是如果页面中通过ajax,动态加载的内容,这种方式是取不到的,于是用到了httpRe ...
- Asp.net中HttpRequest.Params与Reques.Item之异同
今天才注意到HttpRequest.Params与HttpRequest.Item这两个玩意竟然有微妙的不同.上午的时候同事被坑了发现这玩意的说明还真微妙. 场景再现: 前台提交一个POST请求到后台 ...
- HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。
/************************************************************************************** 文 件 名: WebRe ...
- 对象化的Http和请求对象HttpRequest
在面向对象的语言中,有种“万物皆对象”的说法.在上篇文章中介绍了HttpRuntime类,在该类收到请求之后,立即通过HttpWorkerRequest工作者对象对传递的参数进行分析和分解,创建方便网 ...
- ASP.Net核心对象HttpRequest
描述context. Request["username"]; 通过这种方式,能够得到一个HttpRequest对象.HttpRequest对象描述了,关于请求的相关信息,我们可以 ...
- .net学习笔记----HttpRequest类
一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...
- Win7下 httpRequest带证书请求https网站
常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...
随机推荐
- 2017.2.22 activiti实战--第六章--任务表单
学习资料:<Activiti实战> 第六章 任务表单 本章将一步步完成一个协同办公系统(OA)的请假流程的设计,讲解如何将Activiti和实际业务联系起来. 首先讲解动态表单与外置表单的 ...
- Git可视化极简易教程 — Git GUI使用方法
Git可视化极简易教程 — Git GUI使用方法 学习了:http://www.runoob.com/w3cnote/git-gui-window.html
- C#网络编程:Socket编程
套接字简介:套接字最早是Unix的,window是借鉴过来的.TCP/IP协议族提供三种套接字:流式.数据报式.原始套接字.其中原始套接字允许对底层协议直接访问,一般用于检验新协议或者新设备问题,很少 ...
- 报错: Access restriction: The type JPEGImageEncoder is not accessible due to restriction on required library
报错: Access restriction:The type JPEGCodec is not accessible due to restriction on required library C ...
- Github css加载失败,样式混乱解决办法
github被墙的解决办法 Github css加载失败,样式混乱解决办法 打开cmd,输入 nslookup github.com 8.8.8.8 ,下面就会显示出github的服务器地址列 ...
- project修改时间日历
视图→甘特图 格式→时间表→右键时间表 详细的日程表,然后双击时间即可
- 如何修改mindmanager默认字体为微软雅黑
mindmanager默认的字体是Verdana的,如何改为大家喜欢的其他字体呢?比如微软雅黑. 其实很简单,以我使用的是汉化版Mindmanager2012为例,只需要下面几个步骤 第1步:找到 ...
- <LeetCode OJ> 141 / 142 Linked List Cycle(I / II)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- HTML5 2D平台游戏开发#4状态机
在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...
- js的常用小技巧
//类对象转成数组 var domNodes = Array.prototype.slice.call(document.getElementsByTagName("*")); ...