public void postMultipartDataForm(string pUrl, Dictionary<string, string> pHeaders,
Dictionary<string, string> pPostParams, HttpFormFile pFormFile,
ProgressHandler pProgressHandler, CompletionHandler pCompletionHandler)
{
if (pFormFile == null)
{
if (pCompletionHandler != null)
{
pCompletionHandler(ResponseInfo.fileError(new Exception("no file specified")), "");
}
return;
}
HttpWebRequest vWebReq = null;
HttpWebResponse vWebResp = null;
try
{
vWebReq = (HttpWebRequest)WebRequest.Create(pUrl);
vWebReq.ServicePoint.Expect100Continue = false;
}
catch (Exception ex)
{
if (pCompletionHandler != null)
{
pCompletionHandler(ResponseInfo.invalidRequest(ex.Message), "");
}
return;
}
try
{
vWebReq.UserAgent = this.getUserAgent();
vWebReq.AllowAutoRedirect = false;
vWebReq.Method = "POST";
//create boundary
string formBoundaryStr = this.createFormDataBoundary();
string contentType = string.Format("multipart/form-data; boundary={0}", formBoundaryStr);
vWebReq.ContentType = contentType;
if (pHeaders != null)
{
foreach (KeyValuePair<string, string> kvp in pHeaders)
{
if (!kvp.Key.Equals("Content-Type"))
{
vWebReq.Headers.Add(kvp.Key, kvp.Value);
}
}
}
//write post body
vWebReq.AllowWriteStreamBuffering = true;
byte[] formBoundaryBytes = Encoding.UTF8.GetBytes(string.Format("{0}{1}\r\n",
FORM_BOUNDARY_TAG, formBoundaryStr));
byte[] formBoundaryEndBytes = Encoding.UTF8.GetBytes(string.Format("\r\n{0}{1}{2}\r\n",
FORM_BOUNDARY_TAG, formBoundaryStr, FORM_BOUNDARY_TAG));
using (Stream vWebReqStream = vWebReq.GetRequestStream())
{
//write params
if (pPostParams != null)
{
foreach (KeyValuePair<string, string> kvp in pPostParams)
{
vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length);
byte[] formPartTitleData = Encoding.UTF8.GetBytes(
string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n", kvp.Key));
vWebReqStream.Write(formPartTitleData, 0, formPartTitleData.Length);
byte[] formPartBodyData = Encoding.UTF8.GetBytes(string.Format("\r\n{0}\r\n", kvp.Value));
vWebReqStream.Write(formPartBodyData, 0, formPartBodyData.Length);
}
}
vWebReqStream.Write(formBoundaryBytes, 0, formBoundaryBytes.Length);
//write file name
string filename = pFormFile.Filename;
if (string.IsNullOrEmpty(filename))
{
filename = this.createRandomFilename();
}
byte[] filePartTitleData = Encoding.UTF8.GetBytes(
string.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", filename));
vWebReqStream.Write(filePartTitleData, 0, filePartTitleData.Length);
//write content type
string mimeType = FORM_MIME_OCTECT; //!!!注意这里 @fengyh 2016-08-17 15:00
if (!string.IsNullOrEmpty(pFormFile.ContentType))
{
mimeType = pFormFile.ContentType;
}
byte[] filePartMimeData = Encoding.UTF8.GetBytes(string.Format("Content-Type: {0}\r\n\r\n", mimeType));
vWebReqStream.Write(filePartMimeData, 0, filePartMimeData.Length);
//write file data
switch (pFormFile.BodyType)
{
case HttpFileType.FILE_PATH:
try
{
FileStream fs = File.Open(pFormFile.BodyFile, FileMode.Open, FileAccess.Read);
this.writeHttpRequestBody(fs, vWebReqStream);
}
catch (Exception fex)
{
if (pCompletionHandler != null)
{
pCompletionHandler(ResponseInfo.fileError(fex), "");
}
}
break;
case HttpFileType.FILE_STREAM:
this.writeHttpRequestBody(pFormFile.BodyStream, vWebReqStream);
break;
case HttpFileType.DATA_BYTES:
vWebReqStream.Write(pFormFile.BodyBytes, 0, pFormFile.BodyBytes.Length);
break;
case HttpFileType.DATA_SLICE:
vWebReqStream.Write(pFormFile.BodyBytes, pFormFile.Offset, pFormFile.Count);
break;
}
vWebReqStream.Write(formBoundaryEndBytes, 0, formBoundaryEndBytes.Length);
vWebReqStream.Flush();
}
//fire request
vWebResp = (HttpWebResponse)vWebReq.GetResponse();
handleWebResponse(vWebResp, pCompletionHandler);
}
catch (WebException wexp)
{
// FIX-HTTP 4xx/5xx Error 2016-11-22, 17:00 @fengyh
HttpWebResponse xWebResp = wexp.Response as HttpWebResponse;
handleErrorWebResponse(xWebResp, pCompletionHandler, wexp);
}
catch (Exception exp)
{
handleErrorWebResponse(vWebResp, pCompletionHandler, exp);
}
finally
{
if (vWebResp != null)
{
vWebResp.Close();
vWebResp = null;
}
if (vWebReq != null)
{
vWebReq.Abort();
vWebReq = null;
}
}
}