大家好,又见面了,我是你们的朋友全栈君。
使用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();
}
}
}
备忘!
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/159665.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...