upyun官网没有C#的SDK,只好自己动手写。
static void Main(string[] args)
{
string localPath = "D:/Desktop/img/bg_buy.png";
Hashtable headers = new Hashtable();
FileStream fs = new FileStream(localPath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] postArray = r.ReadBytes((int)fs.Length);
HttpWebResponse response = StartHttpProcess("v0.api.upyun.com", "/img21/1.png", postArray, headers, MD5Helper.GetFileMD5(localPath),true);
Stream instream = response.GetResponseStream();
}
public static HttpWebResponse StartHttpProcess(string apiDomain, string romotePath, byte[] postData, Hashtable headers, string contentMD5, bool autoMakeDir)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + apiDomain + "/" + "服务名" + romotePath);
request.Method = "POST";
if (autoMakeDir)
{
headers.Add("mkdir", "true");
}
if (postData != null)
{
request.ContentLength = postData.Length;
request.KeepAlive = true;
if (!string.IsNullOrEmpty(contentMD5))
{
request.Headers.Add("Content-MD5", contentMD5);
}
}
request.Headers.Add("Authorization", "Basic " +
Convert.ToBase64String(new System.Text.ASCIIEncoding().GetBytes("操作员" + ":" + "密码")));
foreach (DictionaryEntry var in headers)
{
request.Headers.Add(var.Key.ToString(), var.Value.ToString());
}
if (postData != null)
{
Stream dataStream = request.GetRequestStream();
dataStream.Write(postData, 0, postData.Length);
dataStream.Close();
}
return (HttpWebResponse)request.GetResponse();
}