[英]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.