开发者

Need help with Windows Phone 7 image upload to webservice

Basically I have a rest webservice that is self hosted on a pc and I want to upload an image from a windows 7 phone. A lot of this is new to me so I have been learning as I go. My problem is that I can not upload an image to the webservice.

Below is the code that I am using.

I am testing with the wifi connection and it is up and working. I am using Fiddler and I can see the requests going to and from the phone/pc

using the emulator I can sort of get this to work (i get a 200 code and can write the image to disk)

When I try to post with the actual device I get a 400 response code back.

Any help I can get would be great. I have been spinning my wheels trying to solve this bug.

//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 = s开发者_StackOverflow中文版er.ReadObject(strm).ToString();
       DisplayMessage(this.txtAction, id);   
    }
    catch (Exception ex)
    {
       DisplayMessage(this.txtError, ex.Message);
    }
 }


It sounds like you have a connectivity issue to resolve. Perhaps try accessing your local service by IP, and check there is no firewall interception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜