Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to upload an image from my windows phone 8 app to the SQL server database using a WebAPI. I am using a model class for the Images, which simply consists of an ID belonging to the item the image is for, the name of the image and a byte array to hold the image itself. I use a WebClient object to access the WebAPI method. When I try to upload the image, it throws the exception as shown below. Does anyone have an idea why it errors? Also, I am open to other methods of storing the image to my SQL database. Thanks for having a look!

Code

private MemoryStream photoStream;

...

private void upload()
        {
            try
            {
                Images image = new Images();
                image.ImagesBytes = photoStream.ToArray();
                image.ImagesID = 3;
                image.ImagesCaption = "this is a test";

                string jsonData = JsonConvert.SerializeObject(image);

                WebClient webClient = new WebClient();
                webClient.Headers["Content-type"] = "application/json";
                webClient.Encoding = Encoding.UTF8;

                Uri uri = new Uri("http://myIP/api/Images/", UriKind.Absolute);
                webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
                webClient.UploadStringAsync(uri, "GET", jsonData);
            }
            catch
            {
                // Display the Uploaded message
                tbError.Visibility = Visibility.Visible;
            }
        }

....

        void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            try
            {
                Images image = JsonConvert.DeserializeObject<Images>(e.Result);
            }
            catch (Exception ex)
            {
                // Display the Uploaded message
                tbError.Visibility = Visibility.Visible;
            }
        }

Exception

System.Net.ProtocolViolationException: Operation is not valid due to the current state of the object.
   at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state)
   at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.UploadDownloadBits(WebRequest request, Stream readStream, Stream writeStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate upCompletionDelegate, CompletionDelegate downCompletionDelegate, AsyncOperation asyncOp)
   at System.Net.WebClient.UploadStringAsync(Uri address, String method, String data, Object userToken)
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I believe your HTTP method is incorrect.

Instead of ...

webClient.UploadStringAsync(uri, "GET", jsonData);

try ...

webClient.UploadStringAsync(uri, "POST", jsonData);
share|improve this answer
 
Wow! Thanks @Rick Rainey, great spot! Can't believe I didn't notice that all this time –  Ayman H Aug 14 '13 at 20:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.