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

本文介绍了一种在HTTPS通道中使用SSL加密传输文件的方法,客户端和服务端的代码实现均被详细展示。通过定义一个回调方法来处理证书验证,使得传输过程安全可靠。

摘要生成于 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没有任何区别,服务端接收处理也完全一样:

//makeData

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

StringBuilderdata
=newStringBuilder("-----------------------------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
=newStringBuilder("/r/n-----------------------------7d8333515d0cfe--/r/n");
byte[]end=Encoding.ASCII.GetBytes(data.ToString());
byte[]buf=newbyte[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);

//endmakedata

System.Net.ServicePointManager.ServerCertificateValidationCallback
=newSystem.Net.Security.RemoteCertificateValidationCallback(MyCallback);
HttpWebRequestrequest
=(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;MSIE6.0;WindowsNT5.1;SV1;.NETCLR2.0.50727;.NETCLR3.0.04506.648;.NETCLR3.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();
HttpWebResponserep
=(HttpWebResponse)request.GetResponse();
StreamReadersr
=newStreamReader(rep.GetResponseStream(),ASCIIEncoding.ASCII);
stringtext=sr.ReadToEnd();
sr.Close();
Console.WriteLine(text);


服务端的接收:

usingSystem;
usingSystem.Web;

publicclassHandler:IHttpHandler...{

publicvoidProcessRequest(HttpContextcontext)...{
stringusername=context.Request.Form.Get("uname");
HttpPostedFilehpf
=context.Request.Files[0];
context.Response.ContentType
="text/html";
context.Response.Output.WriteLine(
"yournameis:{0},Filefrom:{1},ctis:{2},clis:{3}",username,hpf.FileName,hpf.ContentType,hpf.ContentLength);
}


publicboolIsReusable...{
get...{
returnfalse;
}

}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值