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;
}
}

}
