我有一个 Windows Phone 应用程序,我正在尝试将 JSON 格式的数据发布到 WCF 应用程序。尽管建立了连接,但服务器会返回一条自定义消息,其中包含
这是 C# 代码:
ReportSightingRequest.Instance.Source = Source.IPhone;
var jsonData = JsonConvert.SerializeObject(ReportSightingRequest.Instance);
var uri = new Uri("urlGoesHere", UriKind.Absolute);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = jsonData.Length;
string received;
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
using (var responseStream = response.GetResponseStream())
{
using (var sr = new StreamReader(responseStream))
{
received = await sr.ReadToEndAsync();
}
}
}
这是 WCF 接口:
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description("Description.")]
Response.Response ReportSighting(ReportSightingRequest sighting);
这是实现:
public Response ReportSighting(ReportSightingRequest sightingRequest)
{
var response = new Response();
if (sightingRequest == null || sightingRequest.TypeId == null)
{
response.Status = ResponseStatus.InvalidArguments;
response.Message = "Request is null or no type has been supplied.";
return response;
}
...
}
当我从电话中调用 ReportSighting 方法时,我收到“请求为空或未提供类型”消息。奇怪的是,我正在发送 a并且 WP8 端的对象在我发送时绝对不为空。当我在 jsonData 上设置断点时,它包含了所有内容。该对象也与 WCF 应用程序中的对象完全相同。TypeId
sightingRequest
ReportSightingRequest
ReportSightingRequest
几乎感觉对象没有被序列化。这是我唯一能想到的。
有没有人有任何想法/建议?
更新
我注意到我实际上并没有发送对象。Shawn Kendrot 的答案似乎很有意义,但是当我集成他的代码时,它返回一个Not Found错误。
更新 以下代码适用于控制台应用程序:
var jsonData = "a hard coded JSON string here";
var uri = new Uri("a url goes here", UriKind.Absolute);
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.ContentType = "application/json; charset=utf-8";
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)
{
Console.WriteLine(ex.Message);
}
webRequest.BeginGetResponse(
ar2 =>
{
try
{
using (var response = webRequest.EndGetResponse(ar2))
using (var reader = new StreamReader(response.GetResponseStream()))
{
var received = reader.ReadToEnd();
//Console.WriteLine(received);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}, null);
}, null);
更新
我已经更改了 WP8 中的代码以匹配 Shawn Kendrot 的解决方案。我在这里面临的问题是我收到一条Not Found
错误消息:
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)
{
MessageBox.Show("Unsuccessful");
}
webRequest.BeginGetResponse(
ar2 =>
{
try
{
using (var response = webRequest.EndGetResponse(ar2))
using (var reader = new StreamReader(response.GetResponseStream()))
{
var received = reader.ReadToEnd();
}
}
catch (Exception ex)
{
MessageBox.Show("Unsuccessful");
}
}, null);
}, null);
我得到一个:
{System.UnauthorizedAccessException:无效的跨线程访问。在 MS.Internal.XcpImports.CheckThread() 在 MS.Internal.XcpImports.MessageBox_ShowCore(String messageBoxText, String caption, UInt32 type) 在 System.Windows.MessageBox.ShowCore(String messageBoxText, String caption, MessageBoxButton button) 在 System.Windows .MessageBox.Show(String messageBoxText) at Notify.Logic.WebServices.<>c_ DisplayClass2.b _1(IAsyncResult ar2) at System.Net.Browser.ClientHttpWebRequest.<>c_ DisplayClass1d.b _1b(Object state2)}
当我尝试做 `MessageBox.Show(ex.Message);
更新
我已经解决了 MessageBox.Show 错误消息的问题。
该webRequest.Headers
对象具有以下内容:
{Content-Type: application/json; charset=utf-8;}