#region 文件上传。
/// <summary>
/// 文件上传。
/// </summary>
public async Task<string> UpLoadFilesAsync(string fileName, string token)
{
string fileType = Path.GetExtension(fileName);
string fileExpand = fileType.Substring(1, fileType.Length - 1);
string upLoadParms = "?Service=notifyfile" + "&token=" + token + "&filetype=" + fileExpand;
WebRequest webRequest = HttpWebRequest.Create(AppConfig.HttpISSServiceUrl + upLoadParms);
System.Text.Encoding coding = System.Text.Encoding.UTF8;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
System.IO.Stream requestStream = webRequest.GetRequestStream();
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream, System.Text.Encoding.UTF8);
byte[] buffer = binaryReader.ReadBytes((int)fileStream.Length);
requestStream.Write(buffer, 0, buffer.Length);
fileStream.Close();
requestStream.Close();
WebResponse response = webRequest.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader streamReador = new System.IO.StreamReader(responseStream);
string returnVal = streamReador.ReadToEnd();
responseStream.Close();
response.Close();
return AnalyzeXML(returnVal);
}
/// <summary>
/// 结果为通知格式,返回true为发送成功
/// </summary>
private string AnalyzeXML(string xml)
{
if (string.IsNullOrEmpty(xml)) return "";
try
{
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
XmlNode rootNode = xmlDoc.DocumentElement;
string fileGuid = rootNode.SelectSingleNode("filename").InnerText.Trim();
return fileGuid;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
原文:http://www.cnblogs.com/changbaishan/p/5482646.html