HTTPS传送文件的打包方式(为一个朋友调试的过程)

本文介绍了一种无需客户端证书验证的HTTPS通信方式,并提供了具体的.NET实现示例,包括客户端发送请求和服务器端处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https通道如果服务端忽略客户证书(也就是不要求客户提交证书,只是通讯过程用SSL对数据加密传输)的话,
在应用层和普通HTTP没有区别,因为加密的是协议层,你客户端应用程序打包和服务端处理的逻辑不需要改变
(仅仅是加一个验证方法而已)  
先定义一个回调方法:        
public static bool MyCallback(Object sender,
 X509Certificate certificate,
 X509Chain chain,
 System.Net.Security.SslPolicyErrors sslPolicyErrors )       
 {            return true;         }
然后在传输之前调用: System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(MyCallback);
就行了,下面和普通HTTP没有任何区别,服务端接收处理也完全一样:

 

 

           //make Data

            FileStream fs 
= new FileStream("d://a.gif", FileMode.Open);
            
byte[] tf = new byte[(int)fs.Length];
            fs.Read(tf, 
0, (int)fs.Length);
            fs.Close();

            StringBuilder data 
= new StringBuilder("-----------------------------7d8333515d0cfe/r/n");
            data.Append(
"Content-Disposition: form-data; name=/"uname/"/r/n/r/n");
            data.Append(
"axmen/r/n");
            data.Append(
"-----------------------------7d8333515d0cfe/r/n");
            data.Append(
"Content-Disposition: form-data; name=/"passwd/"/r/n/r/n");
            data.Append(
"111111/r/n");
            data.Append(
"-----------------------------7d8333515d0cfe/r/n");
            data.Append(
"Content-Disposition: form-data; name=/"myfile/"; filename=/"D://a.gif/"/r/n");
            data.Append(
"Content-Type: image/gif/r/n/r/n");
            
byte[] tmp = Encoding.ASCII.GetBytes(data.ToString());
            data 
= new StringBuilder("/r/n-----------------------------7d8333515d0cfe--/r/n");
            
byte[] end = Encoding.ASCII.GetBytes(data.ToString());
            
byte[] buf = new byte[tmp.Length + tf.Length + end.Length];
            Array.Copy(tmp, 
0, buf, 0, tmp.Length);
            Array.Copy(tf, 
0, buf, tmp.Length, tf.Length);
            
//澶氫釜鏂囦欢閲嶅涓婇潰鐨勬墦鍖呰繃绋?
            Array.Copy(end, 0, buf, tmp.Length + tf.Length, end.Length);
            
            
//end make data

            System.Net.ServicePointManager.ServerCertificateValidationCallback 
= new System.Net.Security.RemoteCertificateValidationCallback(MyCallback);
            HttpWebRequest request 
= (HttpWebRequest)WebRequest.Create("https://192.168.3.28:81/MyWebSite/Handler.ashx");
            request.Accept 
= "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight, application/x-silverlight-2-b1, */*";
            request.ContentType 
= "multipart/form-data; boundary=---------------------------7d8333515d0cfe";
            request.ContentLength 
= buf.Length;
            request.UserAgent 
= "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            request.Headers.Add(
"Accept-Language: zh-cn");
            request.Headers.Add(
"Accept-Encoding: gzip, deflate");
            
//request.Headers.Add("Host: www.XXXX.com");
            request.Method = "POST";
            request.GetRequestStream().Write(buf, 
0, buf.Length);
            request.GetRequestStream().Flush();
            request.GetRequestStream().Close();
            HttpWebResponse rep 
= (HttpWebResponse)request.GetResponse();
            StreamReader sr 
= new StreamReader(rep.GetResponseStream(), ASCIIEncoding.ASCII);
            
string text = sr.ReadToEnd();
            sr.Close();
            Console.WriteLine(text);

 


服务端的接收:

 

using System;
using System.Web;

public class Handler : IHttpHandler {
    
    
public void ProcessRequest (HttpContext context) {
        
string username = context.Request.Form.Get("uname");
        HttpPostedFile hpf 
= context.Request.Files[0];
        context.Response.ContentType 
= "text/html";
        context.Response.Output.WriteLine(
"your name is: {0},File from :{1},ct is :{2},cl is :{3}",username,hpf.FileName,hpf.ContentType,hpf.ContentLength);
    }

 
    
public bool IsReusable {
        
get {
            
return false;
        }

    }


}

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值