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的更多相关文章

  1. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

  2. 防刷票机制研究和.NET HttpRequest Proxy

    最近应朋友之约 测试他做的投票网站 防刷票机制能力如何,下面有一些心得和体会. 朋友网站用PHP写的,走的是HttpRequest,他一开始认为IP认证应该就差不多了.但说实话这种很low,手动更换代 ...

  3. python httprequest, locust

    r = self.client.get("/orders", headers = {"Cookie": self.get_user_cookie(user[0] ...

  4. c# WebBrower 与 HttpRequest配合 抓取数据

    今天研究一个功能,发现一个问题. 通过webbrower模拟用户自动登录可以完成,并且可以取到相对应的页面内容. 但是如果页面中通过ajax,动态加载的内容,这种方式是取不到的,于是用到了httpRe ...

  5. Asp.net中HttpRequest.Params与Reques.Item之异同

    今天才注意到HttpRequest.Params与HttpRequest.Item这两个玩意竟然有微妙的不同.上午的时候同事被坑了发现这玩意的说明还真微妙. 场景再现: 前台提交一个POST请求到后台 ...

  6. HttpRequest重写,解决资源战胜/链接超时/分块下载事件通知 问题。

    /************************************************************************************** 文 件 名: WebRe ...

  7. 对象化的Http和请求对象HttpRequest

    在面向对象的语言中,有种“万物皆对象”的说法.在上篇文章中介绍了HttpRuntime类,在该类收到请求之后,立即通过HttpWorkerRequest工作者对象对传递的参数进行分析和分解,创建方便网 ...

  8. ASP.Net核心对象HttpRequest

    描述context. Request["username"]; 通过这种方式,能够得到一个HttpRequest对象.HttpRequest对象描述了,关于请求的相关信息,我们可以 ...

  9. .net学习笔记----HttpRequest类

    一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...

  10. Win7下 httpRequest带证书请求https网站

    常规情况下创建Web请求,并获取请求数据的代码如下: WebRequest req = WebRequest.Create(url); req.Timeout = 15000; WebResponse ...

随机推荐

  1. AngularJS中Route例子

    代码:https://files.cnblogs.com/files/xiandedanteng/angularJSRouteSample.rar 点击‘首页’后: 点击‘电脑’后: <!DOC ...

  2. java查看工具jinfo-windows

    Generates configuration information. This command is experimental and unsupported. Synopsis jinfo [  ...

  3. spring(16)------spring的数据源配置

    在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...

  4. 动态载入Layout 与 论Activity、 Window、View的关系

    1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...

  5. robotframe使用之滚动条

    方法一:Excute JavaScript window.scrollTo(0,document.body.scrollHeight); 方法二:Execute javascript document ...

  6. 中兴应用之星携手天翼开放平台:让APP开发更简单

    日前,业内率先的APP开发平台运营商中兴应用之星与中国电信天翼开放平台达成战略合作.即广大用户通过天翼开放平台,可直接享受到应用之星提供的"APP开发服务".   应用之星.中兴通 ...

  7. 最短路 uva12661 Funny Car Racing

    传送门:点击打开链接 题意:给你有向图,每条边呈周期性开放,即开放a时间,再关闭b时间.再开放a时间以此类推 假设时间不足以穿过这条路则不能走.你能够在节点等待时间,问从s走到t所须要的最小时间 细致 ...

  8. anaconda2下面安装opencv2.4.13.4完成----解决默认安装的问题----Thefunction is not implemented. Rebuild the library with Windows, GTK+ 2.x orCarbon support. If you are on Ubuntu or Debian, install libgtk2.0‑dev and pkg

    转载自:http://blog.csdn.net/qingyanyichen/article/details/73550924 本人下载编译安装了opencv2.4.9,oppencv2.4.10,o ...

  9. 【Java】使用@Value @Reource或@Autowire依赖 (值) 注入时出现NPE的排查方法

    首先想说明的是,@Value @Resource和@Autowire虽然都是用于依赖注入的Annotation,但是二者是有区别的. 1 Resource不依赖于Spring,后者相反,因此为了减少以 ...

  10. 下一代Apache Hadoop MapReduce框架的架构

    背景 随着集群规模和负载增加,MapReduce JobTracker在内存消耗,线程模型和扩展性/可靠性/性能方面暴露出了缺点,为此需要对它进行大整修. 需求 当我们对Hadoop MapReduc ...