开发者

JSON Data posted by Silverlight is not reaching the server

I have a web client I'm creating in Silverlight. I am trying to get it to communicate it with my web services on my server through GET and POST requests and JSON. The GET requests work fine and I'm able to parse the JSON on the Silverlight end. The POST requests however dont seem to work. The server reads that there is a POST request, but the POST array is empty.

Ive tried two pieces of code to send the POST requests, but both are resulting in the same response - an empty array.

The first Silverlight code I tried was:

    public MainPage()
    {
        InitializeComponent();
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri("http://www.dipzo.com/game/services.php"));
        request.Method = "POST";
        request.ContentType = "application/json";
        request.BeginGetRequestStream(new AsyncCallback(OnGetRequestStreamCompleted), request);
    }

    private void OnGetRequestStreamCompleted(IAsyncResult ar)
    {
        HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
        using (StreamWriter writer = new StreamWriter(request.EndGetRequestStream(ar)))
        {
            writer.Write("name=david");
        }
        request.BeginGetResponse(new AsyncCallback(OnGetResponseCompleted), request);
    }

    private void OnGetResponseCompleted(IAsyncResult ar)
    {
        //this.GetResponseCoimpleted.Visibility = Visibility.Visible;

        // Complete the Flickr request and marshal to the UI thread
        using (HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)ar.AsyncState).EndGetResponse(ar))
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string results = reader.ReadToEnd();
            }
        }
    }

The second piece I tried was:

    private void WebClient_Click(object sender, RoutedEventArgs e)
    {
        Test t1 = new Test() { Name = "Civics", Marks = 100 };
        DataContractJsonSerializer jsondata = new DataContractJsonSerializer(typeof(Test));
        MemoryStream mem = new MemoryStream();
        jsondata.WriteObject(mem, t1);
        string josnserdata = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
        WebClient cnt = new WebClient();
        cnt.UploadStringCompleted += new UploadStringCompletedEventHandler(cnt_UploadStringCompleted);
        cnt.Headers["Content-type"] = "application/json";
        cnt.Encoding = Encoding.UTF8;
        cnt.UploadStringAsync(new Uri("http://开发者_如何学JAVAwww.dipzo.com/game/services.php"), "POST", josnserdata);
    }

    void cnt_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        var x = e;
    }

The code on the server to consume the service is in PHP and is essentially: var_dump($_POST)

This should output whatever is coming into the post array. I've tested it with a simple PHP client and it works. Just can't get it to work in silverlight. In silverlight I just keep getting an empty array.


You should change the Content-type to application/x-www-form-urlencoded and not application/json which is not a known type yet.


Not that I think anyone is still paying attention tot his old question, but I'm betting that the problem was that it actually WAS getting to the server, but that the server routed the result back to the SL application. This is the behavior I'm seeing with a similar situation from SL5 usingWebClient.UploadStringAsync.

I'm about to implement/test a technique I ran across yesterday which uses a dynamically built, "real" page post from SL; I'll report my findings shortly.

UPDATE -- THIS SOLUTION WORKS: http://www.codeproject.com/Tips/392435/Using-HTTP-Form-POST-method-to-pass-parameters-fro

I've just tested it in my application (SL5 inside MVC) and it works just fine. Make sure you check the HttpContext.Request.Form["fieldname"] to get the value(s) that you want. I used this technique to submit JSON and was able to return a generated Word document for the user.

Once I implemented this I was able to get rid of the unnecessary WebClient that I was attempting to use before.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜