HttpRequest 工具

1.根据 url 和 encoding 获取当前url页面的 html 源代码

  public static string GetHtml(string url, Encoding encoding)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            StreamReader reader = null;
            try
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK && response.ContentLength < 1024 * 1024)
                {
                    if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding);
                    else
                        reader = new StreamReader(response.GetResponseStream(), encoding);
                    string html = reader.ReadToEnd();

                    return html;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {

                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (reader != null)
                    reader.Close();

                if (request != null)
                    request = null;
            }
            return string.Empty;
        }

2.获取URL访问的HTML内容

 public static string GetWebContent(string Url)
        {
            string strResult = "";
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Timeout = 30000;
                request.Headers.Set("Pragma", "no-cache");

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream streamReceive = response.GetResponseStream();

                Encoding encoding = Encoding.GetEncoding("utf-8");
                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                strResult = streamReader.ReadToEnd();
            }
            catch
            {

            }

            return strResult;
        }

3.根据网站url获取流

  public static Stream GetWebUrlStream(string Url)
        {
            Stream stream = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                stream = response.GetResponseStream();
            }
            return stream;
        }

4.根据发送数据获取url 流对象

 public static Stream GetWebUrlStream(string Url, string body)
        {
            Stream stream = null;
            Encoding code = Encoding.GetEncoding("utf-8");
            byte[] bytesRequestData = code.GetBytes(body);
            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
                request.Timeout = 30000;
                request.Method = "post";
                request.ContentType = "application/octet-stream";
                //填充POST数据
                request.ContentLength = bytesRequestData.Length;

                Stream reqStream = request.GetRequestStream();
                reqStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                HttpWebResponse wr = (HttpWebResponse)request.GetResponse();
                if (wr.StatusCode == HttpStatusCode.OK)
                {
                    //在这里对接收到的页面内容进行处理
                    stream = wr.GetResponseStream();
                }
            }
            catch { }
            return stream;
        }

5.根据post 数据填充url 获取html对象

 public static string PostHtml(string Url, string body)
        {
            Encoding code = Encoding.GetEncoding("utf-8");
            byte[] bytesRequestData = code.GetBytes(body);
            string strResult = "";
            try
            {
                //设置HttpWebRequest基本信息
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url);
                request.Timeout = 30000;
                request.Method = "post";
                request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

                //填充POST数据
                request.ContentLength = bytesRequestData.Length;

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(bytesRequestData, 0, bytesRequestData.Length);
                }
                using (WebResponse wr = request.GetResponse())
                {
                    //在这里对接收到的页面内容进行处理
                    Stream myStream = wr.GetResponseStream();
                    //获取数据必须用UTF8格式
                    StreamReader sr = new StreamReader(myStream, code);
                    strResult = sr.ReadToEnd();
                }
            }
            catch { }
            return strResult;
        }

6.将本地文件上传到指定的服务器(HttpWebRequest方法)

 public static string PostUpData(string address, string fileNamePath, string saveName, string dataName)
        {
            string returnValue = "";
            FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);//时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("\r\n");
            sb.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", dataName, saveName);
            sb.Append("\r\n");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(300秒)
            httpReq.Timeout = 300000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = fs.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //每次上传4k
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fs.Length))];
                fs.Seek(0, SeekOrigin.Begin);
                int bytesRead = 0;
                while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0)
                    postStream.Write(buffer, 0, bytesRead);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }
            finally
            {
                fs.Close();
            }
            return returnValue;
        }

7.将网址图片上传到指定的服务器(HttpWebRequest方法)

   public static string PostUpDataUrlImg(string address, string fileNamePath, string saveName, string dataName)
        {
            string returnValue = "";
            // 要上传的文件
            System.Drawing.Imaging.ImageFormat format = System.Drawing.Imaging.ImageFormat.Jpeg;
            Image original_image = Image.FromStream(GetWebUrlStream(fileNamePath));
            MemoryStream ms = new MemoryStream();
            original_image.Save(ms, format);
            original_image.Clone();
            //时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("\r\n");
            sb.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", dataName, saveName);
            sb.Append("\r\n");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(30秒)
            httpReq.Timeout = 30000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = ms.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = ms.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //每次上传4k
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)ms.Length))];
                ms.Seek(0, SeekOrigin.Begin);
                int bytesRead = 0;
                while ((bytesRead = ms.Read(buffer, 0, buffer.Length)) != 0)
                    postStream.Write(buffer, 0, bytesRead);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }
            finally
            {
                ms.Close();
            }
            return returnValue;
        }

8.将网络地址文件上传到指定的服务器

  public static string PostHttpUpData(string address, string fileNamehttp, string saveName, string dataName)
        {
            string returnValue = "";
            WebClient wc = new WebClient();
            //网址文件下载
            byte[] downData = wc.DownloadData(fileNamehttp);
            wc.Dispose();
            //时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("\r\n");
            sb.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", dataName, saveName);
            sb.Append("\r\n");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(300秒)
            httpReq.Timeout = 300000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = downData.Length + postHeaderBytes.Length + boundaryBytes.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);

                //发送内容消息
                postStream.Write(downData, 0, downData.Length);

                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);

                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch { }
            return returnValue;
        }

9.将网址图片上传到指定的服务器(HttpWebRequest方法)

  public static string PostUpDataUrlImg(string address, string fileNamePath, string saveName, string dataName, string body)
        {
            string returnValue = "";
            Stream stream = GetWebUrlStream(fileNamePath, body);
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            byte[] WebUrlStream = bytes.ToArray();//时间戳
            string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
            byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
            //请求头部信息
            StringBuilder sb = new StringBuilder();
            sb.Append("--");
            sb.Append(strBoundary);
            sb.Append("\r\n");
            sb.AppendFormat("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"", dataName, saveName);
            sb.Append("\r\n");
            sb.Append("Content-Type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("\r\n");
            string strPostHeader = sb.ToString();
            byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
            //根据uri创建HttpWebRequest对象
            HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(address);
            httpReq.Method = "POST";
            //对发送的数据不使用缓存
            httpReq.AllowWriteStreamBuffering = false;
            //设置获得响应的超时时间(30秒)
            httpReq.Timeout = 30000;
            httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
            long length = WebUrlStream.Length + postHeaderBytes.Length + boundaryBytes.Length;
            long fileLength = WebUrlStream.Length;
            httpReq.ContentLength = length;
            try
            {
                Stream postStream = httpReq.GetRequestStream();
                //发送请求头部消息
                postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
                postStream.Write(WebUrlStream, 0, WebUrlStream.Length);
                //添加尾部的时间戳
                postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
                postStream.Close();
                //获取服务器端的响应
                WebResponse webRespon = httpReq.GetResponse();
                Stream s = webRespon.GetResponseStream();
                StreamReader sr = new StreamReader(s);
                //读取服务器端返回的消息
                returnValue = sr.ReadToEnd();
                s.Close();
                sr.Close();
            }
            catch
            {

            }return returnValue;
        }
时间: 2024-10-28 00:38:43

HttpRequest 工具的相关文章

slim

Slim 是一个非常优雅的 PHP 微框架,非常适合做API,支持多种http请求方式,比如get,post,delete,put等 安装使用Composer composer require slim/slim vendor\slim\slim\index.php <?php require 'Slim/Slim.php'; \Slim\Slim::registerAutoloader(); $app = new \Slim\Slim(); // GET route $app->get( '

树莓派学习笔记——Restful服务 采用slim php apache

0.前言 前些时间沉迷于Restful,采用PHP+Slim+MySQL实现了一些简单的API函数.但是这些工作都是在windows中实现(采用wamp server集成安装包),但是转到linux中还确实有些不一样,下面就使用树莓派尝试一把. 1.安装php和apache2 在这里仅安装php和apache2,如果需要安装mysql请再增加php5-mysql和mysql-server sudo apt-get update sudo apt-get install apache2 php 2

微信小程序:java后台获取openId

一.功能描述 openId是某个微信账户对应某个小程序或者公众号的唯一标识,但openId必须经过后台解密才能获取(之前实现过前台解密,可是由于微信小程序的种种限制,前台解密无法在小程序发布后使用) 二.实现流程 1. 获取微信用户的登录信息: 2. 将encryptedData中的数据作为参数传给java后台 3. java后台进行解密 三.代码实现 1. 后台的解密代码 1 /** 2 * decoding encrypted data to get openid 3 * 4 * @para

Android开发常用工具类

来源于http://www.open-open.com/lib/view/open1416535785398.html 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括  HttpUtils.DownloadManagerPro.Safe.ijiami.ShellUtils.PackageUtils. PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils. ParcelUtils.Rand

js常用工具库XCLJsTool v1.0发布

最近有空,整了一个js的工具库,还没有正式用于项目中,自己也没有时间写测试用例,想了一下,还是贴出来给大家看看,如果有问题,请留言,非常感谢!项目我放在了github上面,会经常更新的,过段时间会发布一版! /** * 欢迎使用本程序,您可以任意修改.复制.分享本程序所有代码,只需要保留本注释即可,谢谢! * 项目地址:<span style="color:#ff0000;">https://github.com/xucongli1989/XCLJsTool</spa

android HttpClient网络通信工具类基于XML

/** * 用于建立于服务器之间通信的工具 * * * */ public class HttpClientAdapter { private HttpClient client; private HttpRequest request; private HttpGet get; private HttpPost post; private HttpResponse response; public HttpClientAdapter() { //设置client client=new Defa

[工具-006] C#如何模拟发包登录

最近接到一个任务,就是模拟某个贴吧的登录发帖功能,我的思路是通过IE浏览器的工具对登陆操作进行抓包,记录登录时候请求的URL,请求方式,请求正文等信息进行模拟的发包. 1.首先我们要到登陆页面,以摇篮网为例子,用IE打开.http://user.yaolan.com/Login.aspx,我们按F12,然后选择网络,点击三角形进行开始监控,然后我们在登陆页面开始操作.接着我们就可以获得到下图的信息. 2.我们抓取到登录的链接,然后点击详细信息,我们可以得到下图 3.有了标头,我们也需要请求的正文

Android常用的工具类

主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.SerializeUtils.S

Android高手速成--第二部分 工具库

主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过依赖注入减少View.服务.资源简化初始化,事件绑定等重复繁琐工作 AndroidAnnotations(Code Diet)android快速开发框架项目地址:https://github.com/excilys/androidannotations文档介绍:https://github.com/excilys/a