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.21 activiti实战--第七章--Activiti与spring集成(一)配置文件
学习资料:<Activiti实战> 第七章 Activiti与容器集成 本章讲解activiti-spring可以做的事情,如何与现有系统集成,包含bean的注入.统一事务管理等. 7.1 ...
- 2016.7.12 在navicat中用sql语句建表
参考资料: http://jingyan.baidu.com/article/f0e83a25a8c4b022e5910116.html 即新建query,然后run. (1)点击要新建表的位置,选择 ...
- iframe 实现网页本页显示
<el-dialog title="" :visible.sync="dialogVisible"> <iframe src="ht ...
- apue学习笔记(第十七章 高级进程间通信)
本章介绍一种高级IPC---UNIX域套接字机制,并说明它的应用方法 UNIX域套接字 UNIX域套接字用于在同一台计算机上运行的进程(无关进程)之间的(全双工)通信.相比于因特网套接字,UNIX域套 ...
- javascript 关于弹出新页面始终在正中央方法
记录一个关于弹出新页面始终在正中央方法 function openwindow(url, name, iWidth, iHeight) { var url; ...
- 使用robotframework常见的几个问题
问题1:用rf运行IEdriver的速度好慢,比如在输入框输入用户名,一秒输入一个字符.你们的是不是这样子的?.如果是chromedriver就很快 解决办法:把IEDriver从64位换成32位 , ...
- u-boot 学习系列 1 - SPL
u-boot这个东西从自我N年前使用到现在,变化好多,今天开始重新研究下,本系列的研究都是基于BeagleBoneBlack(bbb)开发板和 u-boot v201801版本的. SPL介绍 在源代 ...
- 黑名单机制来临,你的应用还好么?Android P DP2最新兼容性报告出炉
5月9日,谷歌面向全球开发者发布了 Android P Beta,即 Android P DP2. 华为终端开放实验室第一时间对TOP1000主流应用兼容性进行测试,那么在版本兼容性方面各主流应用有何 ...
- IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。 HTTP 错误 500.19
因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改.运行命令行 %windir%\system32\inetsrv\appcmd unlock conf ...
- centOS中修改语言环境
在终端中打开 键入 vim ~/.bashrc 在最后一行键入 export LANG="zh_CH.UTF-8"