我无法在Windows手机中发布图像,这会转换为字节数组,这里是mycode -
var client = new WebClient();
client.DownloadStringCompleted += dataSubmitCompleted;
client.DownloadStringAsync(new Uri(
"here url/Service1.svc/saverecord3" + convertImageToArray(bitmapImage1),
UriKind.Absolute));
这里convertImageToArray是将照片转换为字节数组的方法
答案 0 :(得分:0)
您不应该在GET请求中提交图像,这是DownloadStringAsync
所做的。正如@radoslaf所提到的,您应该使用 Upload 方法(默认情况下会执行POST)。如果您要发送字节数组,则应使用UploadDataAsync
(而不是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;
});
}