【问题标题】:Send image in WP7在 WP7 中发送图像
【发布时间】:2011-08-13 00:54:29
【问题描述】:

我需要使用参数“access_token”和“photo”(我需要发送的图像)将图像(jpeg、png 等)发送到服务器。

..if (e.TaskResult == TaskResult.OK) {
                BitmapImage image = new BitmapImage();
                image.SetSource(e.ChosenPhoto);
                //  foto.Source = image;
                Byte[] byteArray;
                using (MemoryStream ms = new MemoryStream()) {
                    WriteableBitmap btmMap = new WriteableBitmap(image);

                    // write an image into the stream
                    System.Windows.Media.Imaging.Extensions.SaveJpeg(btmMap, ms, image.PixelWidth, image.PixelHeight, 0, 100);

                    byteArray = ms.ToArray();
                }
((App)Application.Current).get_data(uploadServer + "?photo=" + *HOW I CAN SEND BYTE ARRAY?* + "&access_token=" + ((App)Application.Current).access_token

//

public void get_data(string url,Action<string> qw) {
            try {
                var request = (HttpWebRequest)WebRequest.Create(
                    new Uri(url));
                request.BeginGetResponse(r => {
                    var httpRequest = (HttpWebRequest)r.AsyncState;
                    var httpResponse = (HttpWebResponse)httpRequest.EndGetResponse(r);
                    HttpStatusCode st = httpResponse.StatusCode;
                    if (st == HttpStatusCode.NotFound) {
                        ToastPrompt toast = new ToastPrompt();
                        toast.TextOrientation = System.Windows.Controls.Orientation.Vertical;
                        toast.Message = "Ошибка соединения с сервером";
                        toast.MillisecondsUntilHidden = 2000;
                        toast.Show();
                    } else
                        using (var reader = new StreamReader(httpResponse.GetResponseStream())) {
                            var response = reader.ReadToEnd();
                            Deployment.Current.Dispatcher.BeginInvoke(new Action(() => {
                                qw(response);
                            }));

                        }
                }, request);
            } catch (WebException e) { }
        }

【问题讨论】:

    标签: c# windows-phone-7 post httprequest


    【解决方案1】:

    您不能将图像“获取”到 URL(您试图将数据发布到您不能的 URL),您需要使用 OutputStream“发布”您的图像您的 POST 请求。

    【讨论】:

    • 嗯,你可以,但它必须编码在查询字符串参数或cookie中。 (可以在客户端生成 Cookie)。
    • 是的,但我认为它们有大小限制(例如 8K 或其他东西),这不适合(也不打算)发送图像,但从技术上讲是可能的
    • function "get_data" 正在发出 post 请求。服务器支持接收图像。我如何使用 OutputStream 来做到这一点(如你所说)?
    • 好吧,您不应该写入 URL 查询字符串,因此首先删除该部分(以避免进一步混淆)。其次,我不知道你使用的是哪个库..它不是.NET框架的标准Web客户端......是facebook还是什么?
    • 我使用的是 facebook 等网站的 API,而不是 facebook。我需要将图像(jpg,png,gif)发送到服务器,例如“server_url?photo=my_photo&access_token=key”。
    【解决方案2】:

    WP7 - POST form with an image

    Dictionary<string, object> data = new Dictionary<string, object>()
                        {
                        {"photo", byteArray},
                        };
                        PostSubmitter post = new PostSubmitter() { url = uploadServer, parameters = data };
                        post.Submit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多