我正在创建Windows Phone 8应用。我需要调用PHP Web服务,它返回给我JSON。当我打电话给服务时,我得到了TargetInvocationException
。细节&代码如下。我尝试使用HttpClient
在Windows 8应用程序中调用相同的Web服务,它工作正常。
private void btnLogin_Click(object sender, RoutedEventArgs e)
{
if (IsValidData())
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(string.Format(LOGIN_URL, txtUsername.Text, txtPassword.Password)));
}
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var aa = e.Result; //Exception is here
}
An exception of type 'System.Reflection.TargetInvocationException' occurred in System.ni.dll but was not handled in user code
Inner Exception Details:
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 System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result)