WP8 POST JSON到WCF

时间:2013-09-26 09:29:50

标签: json wcf post httpwebrequest windows-phone

我有一个Windows Phone 8应用程序,我将一个对象序列化为JSON并创建一个HTTPWebRequest对象并尝试POST到WCF服务。我面临的问题就是:The remote server returned an error: NotFound

这是WP8中的代码:

var jsonData = JsonConvert.SerializeObject(ReportSightingRequest.Instance);
var uri = new Uri("URLGoesHere");
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = jsonData.Length;
webRequest.BeginGetRequestStream(ar =>
{
    try
    {
        using (var os = webRequest.EndGetRequestStream(ar))
        {
            var postData = Encoding.UTF8.GetBytes(jsonData);
            os.Write(postData, 0, postData.Length);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex);
    }

    webRequest.BeginGetResponse(
        ar2 =>
        {
            try
            {
                using (var response = webRequest.EndGetResponse(ar2))
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var received = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }, null);
}, null);

这是在输出窗口中吐出的内容:

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll
An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at [Namespace].<>c__DisplayClass2.<ReportSighting>b__1(IAsyncResult ar2)

WCF服务正常,因为有其他设备使用它。使用fiddler调用请求也有效,控制台应用程序成功没问题。

任何人都可以看到我发生了可怕的错误吗?

0 个答案:

没有答案