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. 海量数据插入数据库效率对照測试 ---ADO.NET下SqlBulkCopy()对照LINQ 下InsertAllOnSubmit()

    摘要:使用.NET相关技术向数据库中插入海量数据是经常使用操作.本文对照ADO.NET和LINQ两种技术.分别使用SqlBulkCopy()和InsertAllOnSubmit()方法进行操作. 得出 ...

  2. 转: Gradle:Gradle入门

    from: http://blog.csdn.net/p106786860/article/details/50422463

  3. C#控件之TreeView

    设置属性  treeView1.HideSelection = false;  当控件没有焦点时仍然突出显示 默认显示色为灰色 设置属性  treeView1.DrawMode = TreeViewD ...

  4. 安装nagios

    第二部分.apache的安装 615  tar zxvf httpd-2.2.9.tar.gz   616  cd httpd-2.2.9  617   ./configure --prefix=/u ...

  5. 利用window.navigator.userAgent判断当前是否微信内置浏览器

    <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. Qt on Android:将Qt调试信息输出到logcat中

    版权全部 foruok .如需转载敬请注明出处(http://blog.csdn.net/foruok). 假设你在目标 Android 设备上执行了 Qt on Android 应用,你可能希望看到 ...

  7. oracle中视图V$PGA_TARGET_ADVICE的用法

    看一下这个视图能给我们带来什么样的信息(视图中每个列都很有帮助):sys@ora10g> SELECT   pga_target_for_estimate / 1024 / 1024 " ...

  8. C++第4次实验(提高班)—继承和派生1

    从项目2和项目3中选1题作为实验.剩下2题写成作业. [项目1 - 龙三] 请在以下程序的横线处填上适当内容,以使程序完整,并使程序的输出为: Name: 龙三 Grade: 19 #include ...

  9. [Linux] 网络

    如何在网络中标识一台计算机 IP 多个程序如何不冲突 通信端口 不同的计算机如何通信 协议 IP A类:0+7位网络号+24位主机号,可用网络2^7-2个,每个网络可容纳2^24-2个主机 B类:10 ...

  10. iOS开发入门

    https://github.com/qinjx/30min_guides/blob/master/ios.md 任何C源程序,不经修改,即可通过Objective-C编译器成功编译 Objectiv ...