本文整理汇总了C#中Windows.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# Windows.ToArray方法的具体用法?C# Windows.ToArray怎么用?C# Windows.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows
的用法示例。
在下文中一共展示了Windows.ToArray方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: HttpPostFile
public IAsyncOperation<string> HttpPostFile(string url, IDictionary<string, object> toPost, string filename,string filetype, string fieldname, Windows.Storage.Streams.IBuffer filebuffer)
{
return Task.Run<string>(async () =>
{
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
Encoding ascii = Encoding.GetEncoding("us-ascii");
byte[] boundarybytes = ascii.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.CookieContainer = cookieJar;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=" + boundary;
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
Stream newStream = await request.GetRequestStreamAsync();
foreach (KeyValuePair<string, object> kvp in toPost)
{
newStream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, kvp.Key, kvp.Value.ToString());
byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
newStream.Write(formitembytes, 0, formitembytes.Length);
}
newStream.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, fieldname, filename, filetype);
byte[] headerbytes = Encoding.UTF8.GetBytes(header);
newStream.Write(headerbytes, 0, headerbytes.Length);
//newStream.wr
byte []buffer = filebuffer.ToArray();
newStream.Write(buffer, 0, buffer.Length);
byte[] trailer = ascii.GetBytes("\r\n--" + boundary + "--\r\n");
newStream.Write(trailer, 0, trailer.Length);
Task<WebResponse> taskresponse = request.GetResponseAsync();
HttpWebResponse response = (HttpWebResponse)await taskresponse;
byte[] data = new byte[1024000];
int length = 0;
int cnt = 1;
while (cnt > 0)
{
cnt = await response.GetResponseStream().ReadAsync(data, length, 1024000 - length);
length += cnt;
}
char[] asciiChars = new char[length];
ascii.GetChars(data, 0, length, asciiChars, 0);
string tempStr = new string(asciiChars);
Match m = Regex.Match(tempStr, "<meta.*charset=(.*?)\"");
string code = m.Groups[1].ToString();
if (string.IsNullOrEmpty(code))
{
m = Regex.Match(tempStr, "<\\?xml.*encoding=\"(.*?)\"");
code = m.Groups[1].ToString();
}
Encoding gb;
if (code == "gbk" || code == "gb2312") gb = Encoding.GetEncoding("gb2312");
else gb = Encoding.UTF8;
Encoding ut = Encoding.UTF8;
byte[] utbyte = Encoding.Convert(gb, ut, data, 0, length);
char[] utChars = new char[ut.GetCharCount(utbyte, 0, utbyte.Length)];
ut.GetChars(utbyte, 0, utbyte.Length, utChars, 0);
string res = new string(utChars);
return res;
}).AsAsyncOperation();
}