win rt httpclient upload File

来源:互联网 发布:锥度计算软件 编辑:程序博客网 时间:2024/06/05 18:22

 HttpClient httpClient = new HttpClient();
            HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, url);
            AuthenticationHeaderValue header = CreateBasicAuthenticationHeader("test@", "test");
            httpRequestMessage.Headers.Add("Authorization", header.ToString());
            var response = await httpClient.SendAsync(httpRequestMessage);

====httprequestMessage

http://msdn.microsoft.com/zh-cn/library/system.net.http.httprequestmessage.aspx

header中增加内容。

 

Authorization: Basic QWxhZGluOnNlc2FtIG9wZW4=

 

   private async Task<bool> UploadFileToSkydrive(StorageFile file)
        {
            string accessToken = "xyz";

            try
            {
                var props = await file.GetBasicPropertiesAsync();

                string uploaduri = string.Format("https://apis.live.net/v5.0/me/skydrive/files/{0}?access_token={1}",
                    file.Name, accessToken);

                HttpClientHandler handler = new HttpClientHandler { MaxRequestContentBufferSize = 10000000 };
                HttpClient httpClient = new HttpClient(handler);

                var stream = await file.OpenStreamForReadAsync();
                StreamContent streamContent = new StreamContent(stream, (int)props.Size);

                HttpResponseMessage response= await httpClient.PutAsync(uploaduri, streamContent);
                return response.IsSuccessStatusCode;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

====

Scenario 6 is really powerful. It uses the MultipartFormDataContent class to create a request like a form's POST request.
MultipartFormDataContent form = new MultipartFormDataContent();

You can add multiple types of content to this "form". For example a simple string:
form.Add(new StringContent("mystring"), "formkey");

If we want to simulate a POST request that submits a form with both text values and a file, we can also add file data (as byte array):
var fileContent = new System.Net.Http.ByteArrayContent(bytes);
form.Add(fileContent, "file");

Optionally you can add extra headers:
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
fileContent.Headers.ContentDisposition.FileName = "filename";

 

 

httpClient = new System.Net.Http.HttpClient();
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
HttpResponseMessage response = null;
// POST
if (isPost)
{
MultipartFormDataContent form = getPostForm(paramList);
response = await httpClient.PostAsync(new Uri(url), form);
}

===通过混合以上两类,参数混合,加上设置httpheader的某些属性;设置User-Agent

var req = new HttpClient(handler)var message = new HttpRequestMessage(HttpMethod.Get, url);message.Headers.Add("User-Agent", "myCustomUserAgent");var response = await req.SendAsync(message);

 

=====

request.Content = new StreamContent(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)));request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

=====


http://stackoverflow.com/questions/15673009/windows-8-xaml-app-issue-with-multipartformdatacontent-not-uploading-image


public static async Task<string> UploadImage(string url, StorageFile file)    {        HttpClient httpClient = new HttpClient();        httpClient.DefaultRequestHeaders.Add("Authorization", authKey);        try        {               IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read);            MultipartFormDataContent form = new MultipartFormDataContent();            form.Add(new StringContent("name"), "myphoto");            var content = readStream.AsStream();  ---------------As stream, sometimes we don't use it            form.Add(new StreamContent(content));            HttpResponseMessage response = await httpClient.PostAsync(url, form);            string res = response.Content.ToString();            return res;        }        catch (HttpRequestException hre)        {            return string.Empty;        }        catch (Exception ex)        {            return string.Empty;           }                }


========使用这个和类似的,其实还是有差别,看返回code

 httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", header.ToString());

参考这个 http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/6f2b2ce4-9b47-4c10-8941-1d9d2e56b0b9



 

原创粉丝点击