コード例 #1
0
ファイル: MainPage.xaml.cs プロジェクト: yjlintw/YJToolkit
        private async void init()
        {
            Uri picUri = new Uri("http://upload.wikimedia.org/wikipedia/commons/d/d6/M8876_640_480.jpg");
            Stream stream = await FileConvert.UriToStream(picUri);

            Uri uri = new Uri("http://192.168.0.61/P284/gateway.php");
            var request = new HttpPostRequest(uri);
            request.Data.Add("method", "login");
            request.Data.Add("type", "email");
            request.Data.Add("info", "*****@*****.**");

            request.Data.Add("rect_eye_l", String.Format("{0},{1}", 0, 0));
            request.Data.Add("rect_eye_r", String.Format("{0},{1}", 0, 0));
            request.Data.Add("rect_nose", String.Format("{0},{1}", 0, 0));
            request.Data.Add("rect_mouth", String.Format("{0},{1}", 0, 0));
            request.Files.Add(new HttpPostFile("file", "123", stream, true));

            try
            {
                Debug.WriteLine("post");
                HttpResponse response = await Http.PostAsync(request);
                Debug.WriteLine("wait");
                Debug.WriteLine(response.Response);
            }
            catch (System.Net.WebException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
コード例 #2
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
        private static void WritePostData(Stream stream, string boundary, HttpPostRequest request)
        {
            var boundarybytes = request.Encoding.GetBytes("\r\n--" + boundary + "\r\n");
            var formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";
            if (request.RawData != null)
            {
                throw new Exception("RawData not allowed if uploading files");
            }

            foreach (var tuple in request.Data)
            {
                stream.Write(boundarybytes, 0, boundarybytes.Length);

                var formitem = string.Format(formdataTemplate, tuple.Key, tuple.Value);
                var formitembytes = request.Encoding.GetBytes(formitem);
                stream.Write(formitembytes, 0, formitembytes.Length);
            }

            const string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
            foreach (var file in request.Files)
            {
                stream.Write(boundarybytes, 0, boundarybytes.Length);

                var header = string.Format(headerTemplate, file.Name, file.Filename, file.ContentType ?? "application/octet-stream");
                var headerbytes = request.Encoding.GetBytes(header);

                stream.Write(headerbytes, 0, headerbytes.Length);

                var fileStream = file.Stream;

                if (fileStream == null)
                {
                    var f = StorageFile.GetFileFromPathAsync(file.Path);
                    var t = f.AsTask();
                    t.RunSynchronously();

                    var f2 = f.GetResults().OpenReadAsync();
                    var t2 = f2.AsTask();
                    t2.RunSynchronously();

                    fileStream = f2.GetResults().AsStreamForRead();
                }

                try
                {
                    var buffer = new byte[1024];
                    var bytesRead = 0;
                    while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        stream.Write(buffer, 0, bytesRead);
                    }
                }
                finally
                {
                    if (file.CloseStream)
                        fileStream.Dispose();
                }

                boundarybytes = request.Encoding.GetBytes("\r\n--" + boundary + "--\r\n");
                stream.Write(boundarybytes, 0, boundarybytes.Length);
            }
        }
コード例 #3
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
 private static void WritePostData(Stream stream, HttpPostRequest request)
 {
     var bytes = request.RawData ?? request.Encoding.GetBytes(GetQueryString(request.Data));
     stream.Write(bytes, 0, bytes.Length);
 }
コード例 #4
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
        private static HttpResponse Post(HttpPostRequest req, Action<HttpResponse> action, string method)
        {
            var response = new HttpResponse(req);

            try
            {
                var boundary = "";
                var request = CreateRequest(req);

                if (req.Credentials != null)
                {
                    request.Credentials = req.Credentials;
                }

                response.WebRequest = request;

                if (req.Files.Count == 0)
                {
                    request.ContentType = "application/x-www-form-urlencoded";
                }
                else
                {
                    boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                }

                if (req.Cookies.Count > 0)
                {
                    request.CookieContainer = new CookieContainer();
                    foreach (var c in req.Cookies)
                    {
                        request.CookieContainer.Add(request.RequestUri, c);
                    }
                }

                request.Method = method;
                if (req.ContentType != null)
                {
                    request.ContentType = req.ContentType;
                }

                if (req.Headers.Count > 0)
                {
                    foreach (var item in req.Headers)
                    {
                        request.Headers[item.Key] = item.Value;
                    }
                }

                response.CreateTimeoutTimer(request);
                request.BeginGetRequestStream(delegate(IAsyncResult ar1)
                {
                    try
                    {
                        using (var stream = request.EndGetRequestStream(ar1))
                        {
                            if (req.Files.Count > 0)
                            {
                                WritePostData(stream, boundary, req);
                            }
                            else
                            {
                                WritePostData(stream, req);
                            }
                        }
                        request.BeginGetResponse(r => ProcessResponse(r, request, response, action), request);
                    }
                    catch (Exception ex)
                    {
                        response.Exception = ex;
                        if (action != null)
                            action(response);
                    }
                }, request);
            }
            catch (System.Exception ex)
            {
                response.Exception = ex;
                if (action != null)
                {
                    action(response);
                }
            }

            lock (pendingRequests)
            {
                pendingRequests.Add(response);
            }

            return response;
        }
コード例 #5
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
 public static HttpResponse Put(HttpPostRequest req, Action<HttpResponse> action)
 {
     return Post(req, action, "PUT");
 }
コード例 #6
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
 public static Task<HttpResponse> PostAsync(HttpPostRequest request)
 {
     var task = new TaskCompletionSource<HttpResponse>();
     Post(request, result =>
     {
         if (result.Successful)
             task.SetResult(result);
         else if (result.Canceled)
             task.SetCanceled();
         else
             task.SetException(result.Exception);
     });
     return task.Task;
 }
コード例 #7
0
ファイル: Http.cs プロジェクト: yjlintw/YJToolkit
 public static HttpResponse Delete(HttpPostRequest req, Action<HttpResponse> action)
 {
     return Post(req, action, "DELETE");
 }