【发布时间】: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