使用MultipartFormDataContent上传内存数据到nexus,在System.Net.Http.dll, v2.0.0.0中。
/// <summary>
///HTTP Post使用Multipart_FormData方式上传内存数据到Nexus
/// </summary>
/// <param name="requestUri"></param>
/// <param name="parameters"></param>
/// <param name="strUserName"></param>
/// <param name="strPwd"></param>
/// <param name="iTimeOut"></param>
/// <returns></returns>
public static string HttpPostUseMultipart_FormData(string requestUri, Dictionary<string, string> parameters, string strUserName, string strPwd, int iTimeOut)
{
string httpResult = "";
try
{
string boundary = "----" + DateTime.Now.Ticks.ToString("x");//分隔符
using (var formContent = new MultipartFormDataContent(boundary))
{
formContent.Headers.ContentType.MediaType = "multipart/form-data";
foreach (var keyValuePair in parameters)
{
formContent.Add(new StringContent(keyValuePair.Value, Encoding.UTF8),
String.Format("\"{0}\"", keyValuePair.Key));
}
using (HttpClient client = new HttpClient())
{
// 创建身份认证
AuthenticationHeaderValue authentication = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(strUserName + ":" + strPwd)));
client.DefaultRequestHeaders.Authorization = authentication;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
try
{
httpResult = client.PostAsync(requestUri, formContent).Result.Content.ReadAsStringAsync().Result;
Console.WriteLine(httpResult);
}
catch (Exception ex)
{
throw ex;
}
}
}
return httpResult;
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
备忘!