【问题标题】:Need help with Windows Phone 7 image upload to webservice需要帮助将 Windows Phone 7 图像上传到 Web 服务
【发布时间】:2011-01-19 00:32:40
【问题描述】:

基本上我有一个自托管在电脑上的休息网络服务,我想从 Windows 7 手机上传图像。这对我来说很多都是新的,所以我一直在学习。我的问题是我无法将图像上传到网络服务。
下面是我正在使用的代码。

我正在测试 wifi 连接,它已启动并正常工作。 我正在使用 Fiddler,我可以看到来自电话/PC 的请求

使用模拟器我可以让它工作(我得到一个 200 的代码并且可以将图像写入磁盘)

当我尝试使用实际设备发帖时,我收到了 400 响应代码。

我能得到的任何帮助都会很棒。我一直在努力解决这个问题。

//webserice code
[ServiceContract]

public interface ItestService
{
  [OperationContract, WebInvoke(Method = "POST", UriTemplate = "PostTest/{targetID}")]
  string PostTest(string targetID, Stream image);
}

//from service def

public string PostTest(string targetID, System.IO.Stream image)
{
   string id = WriteImageToDisk(image, targetID);
   return id;
}

//phone code
private void SendImage1()
{
 string address = ServiceURI + "/PostTest";
 Uri baseAddress = new Uri(address);
 var req = HttpWebRequest.Create(baseAddress) as HttpWebRequest;
 req.Method = "POST";
 req.ContentType = "image/jpeg";
 req.BeginGetRequestStream(SendImagePost_Callback, req);
}

private void SendImagePost_Callback(IAsyncResult result)
{
  try
  {
     var req = result.AsyncState as HttpWebRequest;

     using (var strm = req.EndGetRequestStream(result))
     {
        var bytesToWrite = RawContent();
        strm.Write(bytesToWrite, 0, bytesToWrite.Length);
        strm.Flush();
     }

     req.BeginGetResponse(SendImageResponce_Callback, req);
  }
  catch (Exception ex)
  {
     DisplayMessage(this.txtError, ex.Message);
  }

}

 private void SendImageResponce_Callback(IAsyncResult result)
 {      
    try
    {
       var req = result.AsyncState as HttpWebRequest;
       var strm = resp.GetResponseStream();
       var reader = new StreamReader(strm);

       DataContractSerializer ser = new DataContractSerializer(typeof(string));
       string id = ser.ReadObject(strm).ToString();
       DisplayMessage(this.txtAction, id);   
    }
    catch (Exception ex)
    {
       DisplayMessage(this.txtError, ex.Message);
    }
 }

【问题讨论】:

  • 您好,能否阐明其工作/不工作时的条件?你说它在 wifi 上工作,而不是在可能是相同条件的设备上工作。
  • 感谢您的回复。当从模拟器测试应用程序时,一切正常。我可以进行 GET 调用并将图像从手机发布到服务。但是,当我将应用程序部署到设备并对其进行测试时,图像发布不起作用。在设备上测试应用程序我可以验证我有一个连接。我可以很好地对服务进行所有 GET 调用,但是当我尝试将图像从设备发布到服务时,它会失败。

标签: web-services image post windows-phone-7 upload


【解决方案1】:

听起来您有一个连接问题需要解决。也许尝试通过 IP 访问您的本地服务,并检查没有防火墙拦截。

【讨论】:

  • 根据我的测试,我没有任何连接问题。我已经测试了有和没有我的防火墙。电话应用程序对服务进行了多次“GET”调用,包括一个获取大图像文件的调用。我是一个发送大文本消息的 POST 呼叫,这工作得很好。我遇到的唯一问题是当我尝试从手机设备发布图像时。
猜你喜欢
  • 2014-01-27
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2012-05-13
  • 1970-01-01
相关资源
最近更新 更多