[英]WP8 HttpWebRequest Post Not Working
我有一个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方法时,收到“请求为空或没有提供任何类型”消息。 奇怪的是, 我发送TypeId
和sightingRequest
在WP8侧的对象绝对不是空的时候我送它。 当我在jsonData上放置一个断点时,它包含了所有内容。 ReportSightingRequest
对象也与WCF应用程序中的ReportSightingRequest
完全相同。
几乎感觉对象没有被序列化。 那是我唯一能想到的。
有没有人有任何想法/建议?
更新资料
我注意到我实际上没有发送该对象。 Shawn Kendrot的“答案”似乎很有意义,但是当我集成他的代码时,它会返回“ 未找到”错误。
更新以下代码在控制台应用程序中起作用:
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()在System.Windows.MS.Internal.XcpImports.MessageBox_ShowCore(String messageBoxText,字符串标题,UInt32类型)在System.Windows.MessageBox.ShowCore(String messageBoxText,字符串标题,MessageBoxButton按钮)在System.Windows .MessageBox.Show(字符串messageBoxText),位于Notify.Logic.WebServices。<> c_ DisplayClass2.b _1(IAsyncResult ar2),位于System.Net.Browser.ClientHttpWebRequest。<> c_ DisplayClass1d.b _1b(对象state2)}
当我尝试做`MessageBox.Show(ex.Message);时
更新资料
我已经解决了MessageBox.Show错误消息的问题。
webRequest.Headers
对象具有以下内容:
{Content-Type: application/json; charset=utf-8;}
您的sightingRequest为空,因为您没有发送任何数据。 要使用WebRequest发送数据,您需要使用BeginGetRequestStream方法。 此方法使您可以打包数据。
var webRequest= (HttpWebRequest)WebRequest.Create(uri);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = jsonData.Length;
webRequest.BeginGetRequestStream(ar =>
{
try
{
using (Stream os = webRequest.EndGetRequestStream(ar))
{
var postData = Encoding.UTF8.GetBytes(jsonData);
os.Write(postData, 0, postData.Length);
}
}
catch (Exception ex)
{
// Do something, exit out, etc.
}
webRequest.BeginGetResponse(
ar2 =>
{
try
{
using (var response = webRequest.EndGetResponse(ar2))
using (var reader = new StreamReader(response.GetResponseStream()))
{
string received = reader.ReadToEnd();
}
}
catch (Exception ex)
{
// Do something, exit out, etc.
}
}, null);
}, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.