我是xamarin和Rest API的新手。
我正在尝试从Xamrine IOS代码中调用Rest API,如下所示。我的moto是获取列表(从API)并在列表视图(设备)中显示。但这给了我错误“错误的请求”。但是当在POSTMAN中放置相同的URL时,会给我适当的响应。请在这里帮助我。
邮递员打来的电话
URL:http://localhost/MobileAppAPI/
客户代码:
static List<AuditModel> _audits = new List<AuditModel>();
public static string GetAudits()
{
string URL = "http://localhost/MobileAppAPI/api/Values/Get?id=1000";
string data = CreateObject(URL);
return APIHelper.APIHelper.CreateObject(URL);
}
public static string CreateObject(string URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "application/json";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
try
{
using (Stream webStream = request.GetRequestStream())
using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
{
requestWriter.Write("");
}
WebResponse webResponse = request.GetResponse();
using (Stream webStream = webResponse.GetResponseStream())
{
if (webStream != null)
{
using (StreamReader responseReader = new StreamReader(webStream))
{
string response = responseReader.ReadToEnd();
return response;
}
}
}
}
catch (Exception e)
{
Console.Out.WriteLine("-----------------");
Console.Out.WriteLine(e.Message);
return e.Message;
}
return "";
}
API代码:
// GET api/values/5
public string Get(int id)
{
return "value";
}
例外:
Unhandled Exception:
System.Net.WebException: Error: ConnectFailure (Connection refused)