ConnectFailure与Xamarin中的Http请求

时间:2014-10-21 12:18:46

标签: web-services connection xamarin httprequest xamarin-studio

我使用免费版的Xamarin Studio进行一些测试。我特别想尝试连接到Web服务,但在调试应用程序时收到ConnectFailure(网络无法访问)。 这是我的代码

public class MainActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Our code will go here
        // Get our UI controls from the loaded layout
        EditText phoneNumberText = FindViewById<EditText>(Resource.Id.editText1);
        Button translateButton = FindViewById<Button>(Resource.Id.button1);
        Button callButton = FindViewById<Button>(Resource.Id.button2);

        // Disable the "Call" button
        callButton.Enabled = false;

        // Add code to translate number
        string translatedNumber = string.Empty;


                    var rxcui = "198440";
        var request = HttpWebRequest.Create(string.Format(@"http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/{0}/allinfo", rxcui));
        request.ContentType = "application/json";
        request.Method = "GET";

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                Console.Out.WriteLine("Error fetching data. Server returned status code: {0}", response.StatusCode);
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                var content = reader.ReadToEnd();
                if(string.IsNullOrWhiteSpace(content)) {
                    Console.Out.WriteLine("Response contained empty body...");
                }
                else {
                    Console.Out.WriteLine("Response Body: \r\n {0}", content);
                }

                //Assert.NotNull(content);
            }
        }


    }
}

}

我不明白这是由免费版本引起的错误还是我的错误。 请帮我。 感谢

0 个答案:

没有答案