0

我无法在 Windows Phone 中发布转换为字节数组的图像,这是我的代码-

var client = new WebClient();
client.DownloadStringCompleted += dataSubmitCompleted;
client.DownloadStringAsync(new Uri(
    "here url/Service1.svc/saverecord3" + convertImageToArray(bitmapImage1),
    UriKind.Absolute));

这里 convertImageToArray 是将照片转换为字节数组的方法

4

1 回答 1

0

您不应该在 GET 请求中提交图像,这就是这样DownloadStringAsync做的。正如@radoslaf 提到的,您应该改用Upload方法(默认情况下执行 POST)。如果你发送一个字节数组,你应该使用UploadDataAsync(not UploadString)。

您还应该对服务进行编码以从客户端接收字节。http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx上的帖子展示了如何实现这样的服务。

评论后更新- 我明白了,WP7.5 的 WebClient API 真的非常有限。对于这种情况,您可能最好使用该HttpWebRequest类型。下面的代码显示了如何将其用于您的场景。

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        HttpWebRequest req = HttpWebRequest.CreateHttp("http://here.url/service1.svc/saverecord3");
        req.Method = "POST";
        req.Headers[HttpRequestHeader.ContentType] = "application/octet-stream";
        req.BeginGetRequestStream(GetRequestStreamCompleted, req);
    }

    void GetRequestStreamCompleted(IAsyncResult ar)
    {
        byte[] imageBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
        Stream reqStream = req.EndGetRequestStream(ar);
        reqStream.Write(imageBytes, 0, imageBytes.Length);
        reqStream.Close();
        req.BeginGetResponse(GetResponseCompleted, req);
    }

    void GetResponseCompleted(IAsyncResult ar)
    {
        HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
        HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
        this.Dispatcher.BeginInvoke(() =>
        {
            this.txtStatus.Text = "Upload result: " + resp.StatusCode;
        });
    }
于 2013-04-02T16:28:31.613 回答