Xamarin-CrossPlatform POST HttpWebRequest:远程服务器返回错误:NotFound

时间:2016-09-06 09:56:31

标签: c# xamarin httpwebrequest

我在WindowsPhone上打开WebResponse时遇到问题,总是在我打开WebResponse时遇到异常。我尝试了很多不同的方法但是当我使用异步方法时总是会遇到异常。 请帮帮我。

异常:

  

类型' System.Net.WebException'的例外情况发生在System.Net.ni.DLL中但未在用户代码中处理   附加信息:远程服务器返回错误:NotFound。

private async void btLogin_Click(object sender, RoutedEventArgs e) {
        Uri uri = new Uri("http://ip-address/users/User/jan_kowalski/a");
        HttpWebRequest webRequest = WebRequest.Create(uri) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";

        try {
            using (Stream ss = await webRequest.GetRequestStreamAsync()) {
                await ss.WriteAsync(new byte[0], 0, 0);
                await ss.FlushAsync();
            }

            //using (WebResponse webResponse = await webRequest.GetResponseAsync()) {       // <-- Here there is an exception or...
            //    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) {
            //        string data = await reader.ReadToEndAsync();
            //        Log.d(TAG, data);
            //    }
            //}

            webRequest.BeginGetResponse(async (x) => {
                WebResponse webResponse = webRequest.EndGetResponse(x);         // <-- ...or here there is an exception
                StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                string text = await reader.ReadToEndAsync();
                Log.d(TAG, text);
            }, null);

        } catch (WebException exc) {
            Log.e(TAG, exc.Message + "   \n" +
                exc.Source + "\n status:" +
                exc.Status + "\n" +
                exc.Response + "   \n " +
                exc.StackTrace);
        } catch (Exception ex) {
            Log.e(TAG, ex.Message + "\n stackTrace: " + ex.StackTrace);
        }
}

我注意到Android和IOS可以访问更多的方法,同步方法。当他们使用它一切正常时,我从服务器得到正确的答案。但Windows手机上没有同步方法!

            Uri uri = new Uri("http://ip-address/users/User/jan_kowalski/a");
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            try {
                using (Stream stream = webRequest.GetRequestStream()) {
                    stream.Write(new byte[0], 0, 0);
                }
                using (WebResponse stream = webRequest.GetResponse()) {
                    using (StreamReader reader = new StreamReader(stream.GetResponseStream())) {
                        string data = reader.ReadToEnd();
                        Console.WriteLine(data);
                    }
                }
            }catch(Exception ex) {
                Log.e(TAG, null, ex);
            }

1 个答案:

答案 0 :(得分:0)

我建议您尝试使用HttpClient,界面比WebRequest更清晰:

var client = new HttpClient();
var res = await client.PostAsync("http://ip-address/users/User/jan_kowalski/a", new StringContent("hello, world"));
if (res.IsSuccessStatusCode)
{
    var response = await res.Content.ReadAsStringAsync();
}

我无法使用HttpClient重现您的问题。