使用webclient同时post普通表单字段和文件表单字段数据到指定的URL【转】

作者:freethy 发布于:2013-2-18 18:17 Monday

如大家要转载,请保留本人的版权:
    /*
    *Description:在asp.NET代码中同时post普通表单字段和文件表单字段数据到指定的服务器URL。
    *Auther:chongchong2008-天真的好蓝啊
    *MSN:chongchong2008@msn.com
    *QQ:154674958
    *Blog:chongchong2008.cnblogs.com
    *Dates:2012-02-28
    *Copyright:ChongChong2008 YiChang HuBei China
    */

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Net;

namespace HttpRequest
{
    /// <summary>
    /// description:http post请求客户端
    /// author:chongchong2008
    /// last-modified-date:2012-02-28
    /// </summary>
    public class HttpRequestClient
    {
        #region //字段
        private ArrayList bytesArray;
        private Encoding encoding = Encoding.UTF8;
        private string boundary = String.Empty;
        #endregion

        #region //构造方法
        public HttpRequestClient()
        {
            bytesArray = new ArrayList();
            string flag = DateTime.Now.Ticks.ToString("x");
            boundary = "---------------------------" + flag;
        }
        #endregion

        #region //方法
        /// <summary>
        /// 合并请求数据
        /// </summary>
        /// <returns></returns>
        private byte[] MergeContent()
        {
            int length = 0;
            int readLength = 0;
            string endBoundary = "--" + boundary + "--\r\n";
            byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);

            bytesArray.Add(endBoundaryBytes);

            foreach (byte[] b in bytesArray)
            {
                length += b.Length;
            }

            byte[] bytes = new byte[length];

            foreach (byte[] b in bytesArray)
            {
                b.CopyTo(bytes, readLength);
                readLength += b.Length;
            }

            return bytes;
        }

        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="requestUrl">请求url</param>
        /// <param name="responseText">响应</param>
        /// <returns></returns>
        public bool Upload(String requestUrl, out String responseText)
        {
            WebClient webClient = new WebClient();
            webClient.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);

            byte[] responseBytes;
            byte[] bytes = MergeContent();

            try
            {
                responseBytes = webClient.UploadData(requestUrl, bytes);
                responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
                return true;
            }
            catch (WebException ex)
            {
                Stream responseStream = ex.Response.GetResponseStream();
                responseBytes = new byte[ex.Response.ContentLength];
                responseStream.Read(responseBytes, 0, responseBytes.Length);
            }
            responseText = System.Text.Encoding.UTF8.GetString(responseBytes);
            return false;
        }

        /// <summary>
        /// 设置表单数据字段
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="fieldValue">字段值</param>
        /// <returns></returns>
        public void SetFieldValue(String fieldName, String fieldValue)
        {
            string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";
            string httpRowData = String.Format(httpRow, fieldName, fieldValue);

            bytesArray.Add(encoding.GetBytes(httpRowData));
        }

        /// <summary>
        /// 设置表单文件数据
        /// </summary>
        /// <param name="fieldName">字段名</param>
        /// <param name="filename">字段值</param>
        /// <param name="contentType">内容内型</param>
        /// <param name="fileBytes">文件字节流</param>
        /// <returns></returns>
        public void SetFieldValue(String fieldName, String filename, String contentType, Byte[] fileBytes)
        {
            string end = "\r\n";
            string httpRow = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
            string httpRowData = String.Format(httpRow, fieldName, filename, contentType);

            byte[] headerBytes = encoding.GetBytes(httpRowData);
            byte[] endBytes = encoding.GetBytes(end);
            byte[] fileDataBytes = new byte[headerBytes.Length + fileBytes.Length + endBytes.Length];

            headerBytes.CopyTo(fileDataBytes, 0);
            fileBytes.CopyTo(fileDataBytes, headerBytes.Length);
            endBytes.CopyTo(fileDataBytes, headerBytes.Length + fileBytes.Length);

            bytesArray.Add(fileDataBytes);
        }
        #endregion
    }
}

 

 

使用方法:

----------------------------------------------------------------------------------------------------------------
            string url = "http://127.0.0.1/test/upload";//发送到的页面的地址
            string image_path = "c:/test.jpg";
            FileStream fs = new FileStream(image_path, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            byte[] fileBytes = new byte[fs.Length];
            fs.Read(fileBytes, 0, fileBytes.Length);
            fs.Close();
            fs.Dispose();

            HttpRequestClient httpRequestClient = new HttpRequestClient();
            httpRequestClient.SetFieldValue("id", "123");
            httpRequestClient.SetFieldValue("user_name", "chongchong2008");
            httpRequestClient.SetFieldValue("type_id", "1");

            httpRequestClient.SetFieldValue("uploadfile", "test.jpg", "application/octet-stream", fileBytes);
            string responseText;
            bool uploaded = httpRequestClient.Upload(url, out responseText);


===========================================================================
referrer link: http://www.cnblogs.com/dotey/

============================================================================

评论(23) 引用(0) 浏览(2899)

解决 asp.net 伪静态 IIS设置后 直正HTML无法显示的问题

作者:freethy 发布于:2013-2-18 9:51 Monday

asp.net+UrlRewriter来实现网站伪静态,实现伪静态有一些好处,比如对于搜索引 擎更好收录页面,还有一个好处就是隐藏了真实地址的参数,所以有独立服务器的朋友,配置IIS实现伪静态功能,挺不错的哦,至于ASP.NET如何实现伪 静态,可以看下本人写的这篇文章:,现在有一个问题来了,我配置伪静态成功后,而真正的静态文件(html)却访问不了,有点苦脑,不过最终还是找到了解 决方案

第一步:打开Web.config配置文件

第二步:设置如下节点

 

<compilation debug="false">  
    
<buildProviders>  
    
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />  
    
</buildProviders>  
</compilation>  

 

 

然后继续设置如下节点

<httpHandlers>  
      
<add verb="*" path="*.html" type="System.Web.UI.PageHandlerFactory"/>  
</httpHandlers> 

 

 

配置好以上内容,就可以实现asp.net的伪静态html内容与真正的静态文件html就可以访问了

评论(27) 引用(0) 浏览(4281)

MSSQL修改sa用户名

作者:freethy 发布于:2013-2-4 13:51 Monday

MSSQL默认的sa很少有人改动,但是经常有人用工具扫描sa账户,并且爆破密码。

为了加强安全把默认的sa用户更改掉,安全性会有大幅提升。

 

--Alter LOGIN sa DISABLE

Alter LOGIN sa WITH NAME = [新用户名]

评论(21) 引用(0) 浏览(2620)

Powered by emlog 苏ICP备11068260号-3