博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Http操作类
阅读量:6215 次
发布时间:2019-06-21

本文共 8211 字,大约阅读时间需要 27 分钟。

1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Text;  5 using System.Net;  6 using System.IO;  7   8 namespace LongFor.BLL  9 { 10     ///     11     ///  Http操作类    12     ///     13     public static class HttpRequestUtil 14     { 15         private static Encoding DEFAULT_ENCODING = Encoding.GetEncoding("utf-8"); 16         private static string ACCEPT = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; 17         private static string CONTENT_TYPE = "application/x-www-form-urlencoded;charset=utf-8"; 18         private static string USERAGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; msn OptimizedIE8;ZHCN)"; 19         // "application/x-www-form-urlencoded;charset=gb2312"; 20         ///     21         ///  获取网址HTML    22         ///     23         /// 网址     24         /// 
25 public static string GetHtmlContent(string url) 26 { 27 return GetHtmlContent(url, DEFAULT_ENCODING); 28 } 29 30 /// 31 /// 获取网址HTML 32 /// 33 /// 网址 34 ///
35 public static string GetHtmlContent(string url, Encoding encoding) 36 { 37 string html = string.Empty; 38 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 39 request.UserAgent = USERAGENT; 40 request.Credentials = CredentialCache.DefaultCredentials; 41 WebResponse response = request.GetResponse(); 42 using (Stream stream = response.GetResponseStream()) 43 { 44 using (StreamReader reader = new StreamReader(stream, encoding)) 45 { 46 html = reader.ReadToEnd(); 47 reader.Close(); 48 } 49 } 50 return html; 51 } 52 53 /// 54 /// 获取网站cookie 55 /// 56 /// 网址 57 ///
58 public static string GetCookie(string url) 59 { 60 string cookie = string.Empty; 61 WebRequest request = WebRequest.Create(url); 62 request.Credentials = CredentialCache.DefaultCredentials; 63 using (WebResponse response = request.GetResponse()) 64 { 65 cookie = response.Headers.Get("Set-Cookie"); 66 } 67 return cookie; 68 } 69 70 /// 71 /// Post模式浏览 72 /// 73 /// 网址 74 /// 流 75 ///
76 public static byte[] Post(string url, byte[] data) 77 { 78 return Post(null, url, data, null); 79 } 80 81 /// 82 /// Post模式浏览 83 /// 84 /// 服务器地址 85 /// 网址 86 /// 流 87 /// cookieHeader 88 ///
89 public static byte[] Post(string server, string url, byte[] data, string cookieHeader) 90 { 91 if (data == null || data.Length == 0) 92 { 93 throw new ArgumentNullException("data"); 94 } 95 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 96 if (!string.IsNullOrEmpty(cookieHeader)) 97 { 98 if (string.IsNullOrEmpty(server)) 99 {100 throw new ArgumentNullException("server");101 }102 CookieContainer co = new CookieContainer();103 co.SetCookies(new Uri(server), cookieHeader);104 httpWebRequest.CookieContainer = co;105 }106 httpWebRequest.ContentType = CONTENT_TYPE;107 httpWebRequest.Accept = ACCEPT;108 httpWebRequest.Referer = server;109 httpWebRequest.UserAgent = USERAGENT;110 httpWebRequest.Method = "Post";111 httpWebRequest.ContentLength = data.Length;112 using (Stream stream = httpWebRequest.GetRequestStream())113 {114 stream.Write(data, 0, data.Length);115 stream.Close();116 }117 using (HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse())118 {119 using (Stream stream = webResponse.GetResponseStream())120 {121 long contentLength = webResponse.ContentLength;122 byte[] bytes = new byte[contentLength];123 bytes = ReadFully(stream);124 stream.Close();125 return bytes;126 }127 }128 }129 130 private static byte[] ReadFully(Stream stream)131 {132 byte[] buffer = new byte[128];133 using (MemoryStream ms = new MemoryStream())134 {135 while (true)136 {137 int read = stream.Read(buffer, 0, buffer.Length);138 if (read <= 0)139 return ms.ToArray();140 ms.Write(buffer, 0, read);141 }142 }143 }144 145 /// 146 /// Get模式浏览 147 /// 148 /// Get网址 149 ///
150 public static byte[] Get(string url)151 {152 return Get(null, url, null);153 }154 155 /// 156 /// Get模式浏览 157 /// 158 /// Get网址 159 /// cookieHeader 160 /// 服务器地址 161 ///
162 public static byte[] Get(string server, string url, string cookieHeader)163 {164 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);165 if (!string.IsNullOrEmpty(cookieHeader))166 {167 if (string.IsNullOrEmpty(server))168 {169 throw new ArgumentNullException("server");170 }171 CookieContainer co = new CookieContainer();172 co.SetCookies(new Uri(server), cookieHeader);173 httpWebRequest.CookieContainer = co;174 }175 httpWebRequest.Accept = "*/*";176 httpWebRequest.Referer = server;177 httpWebRequest.UserAgent = USERAGENT;178 httpWebRequest.Method = "GET";179 HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse();180 using (Stream stream = webResponse.GetResponseStream())181 {182 long contentLength = webResponse.ContentLength;183 byte[] bytes = new byte[contentLength];184 bytes = ReadFully(stream);185 stream.Close();186 return bytes;187 }188 }189 } 190 }

 

调用方式:

PostData="queuename=roomsync&body={lengths:2,page:-1,datas:[67763C16-D5C7-4297-B73F-00009BA7D178,066F9157-1E7F-474C-9333-0000CBB2FB43]}";

upRoomUrl="http://192.168.33.166:8080/web/queue";

1 ///  2         /// 发送更新房间数据服务 3         ///  4         /// 需要发送的数据 5         /// 数据来源类型 6         public static void SendService(string PostData, string type) 7         { 8             string upRoomUrl = ConfigurationManager.AppSettings.GetValues("upRoomUrl")[0];//计算更新房间数据接口地址 9             if (upRoomUrl != "null")10             {11                 LogWrape.LoggerInfo(logger, type + ",增量计算房间表数据:" + PostData, DateTime.Now.ToString());12                 byte[] bt = System.Text.Encoding.UTF8.GetBytes(PostData);13                 string retMsg = System.Text.Encoding.UTF8.GetString(HttpRequestUtil.Post(upRoomUrl, bt));14                 LogWrape.LoggerInfo(logger, type + ",增量计算房间表数据返回结果:" + retMsg, DateTime.Now.ToString());15             }16         }

 

 

转载于:https://www.cnblogs.com/webczw/p/3731783.html

你可能感兴趣的文章
普通java项目打jar包运行
查看>>
ABBYY FineReader 12中怎样自定义主窗口
查看>>
使用 highlight.js 高亮网站代码
查看>>
笨兔兔的故事——带你了解Ubuntu,了解Linux 第二章 醒来
查看>>
spark sql简单示例
查看>>
CSRF防范简介
查看>>
关于HTTP keep-alive的实验
查看>>
前嗅ForeSpider脚本教程:字段取值脚本
查看>>
分析NTFS文件系统内部结构
查看>>
android程序加载so动态库和jar包
查看>>
nagios插件性能数据显示格式
查看>>
创建临时表的语法
查看>>
数学相关
查看>>
深入理解Magento – 第三章 – 布局,块和模板
查看>>
外来键简介和sql语句
查看>>
Yii用ajax实现无刷新检索更新CListView数据
查看>>
LNMP--Nginx不记录指定文件日志
查看>>
在线聊天系统
查看>>
SQL Server 命名实例改为默认实例
查看>>
浅谈用户行为分析之用户身份识别:cookie 知多少?
查看>>