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 = new FileStream( " d:\\a.gif " ,FileMode.Open);
byte []tf = new byte [( int )fs.Length];
fs.Read(tf, 0 ,( int )fs.Length);
fs.Close();
StringBuilderdata = 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);
// endmakedata
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.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 = new StreamReader(rep.GetResponseStream(),ASCIIEncoding.ASCII);
string text = sr.ReadToEnd();
sr.Close();
Console.WriteLine(text);
FileStreamfs = new FileStream( " d:\\a.gif " ,FileMode.Open);
byte []tf = new byte [( int )fs.Length];
fs.Read(tf, 0 ,( int )fs.Length);
fs.Close();
StringBuilderdata = 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);
// endmakedata
System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.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 = new StreamReader(rep.GetResponseStream(),ASCIIEncoding.ASCII);
string text = sr.ReadToEnd();
sr.Close();
Console.WriteLine(text);
服务端的接收:
using
System;
using System.Web;
public class Handler: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;
}
}
}
using System.Web;
public class Handler: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;
}
}
}