简体   繁体   中英

Silverlight: Web Service Call JSON Error

OK, so I have no idea why this isn't working. I've found 4 different tutorials/examples of calling a JSON web service asynchronously within Silverlight, and all of them use the same format / logic as I am. On the line "using (streamCommunities = request.EndGetRequestStream(asyncResult))" it is throwing the error I attached at the bottom, an ArgumentException saying it doesn't like the asyncResult. Why is this if every other example I've found uses the same logic?

    private void GetSource(object state)
    {
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(MyValidUri));
    request.Method = "POST";
    request.ContentType = "application/json";
    request.BeginGetResponse(new AsyncCallback(ReadCommunityCallBack), request);
    }

    private void ReadCommunityCallBack(IAsyncResult asyncResult)
    {            
        HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;

        using (Stream outStream = request.EndGetRequestStream(asyncResult))
        {
          // DO STUFF HERE
        }
    }

And the error is here:

System.ArgumentException was unhandled by user code
Message=Value does not fall within the expected range.
   StackTrace:
   at System.Net.Browser.ClientHttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
   at cenTabbedFeed.MainPage.ReadCommunityCallBack(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass1a.<InvokeGetResponseCallback>b__18(Object state2)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Like I said, I've been scouring examples and trying to find an answer on MSDN and I'm stuck and frustrated.

-Thanks in advance RJ

your request method is POST but you don't post anything. Either write something to RequestStream or change your method to GET .

--EDIT--

You can post some string to server as below

byte[] buf = Encoding.UTF8.GetBytes("key=value&key2=value2&key3=value3");
request.ContentType = "application/www-form-urlencoded";
request.ContentLength = buf.Length;
request.GetRequestStream().Write(buf,0,buf.Length);

Uve got the wrong one. Ur using BeginGetResponse . I just had same problem in ACW. changed it to BeginGetRequestStream instead, all is well :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM