我瞭解我發佈了一個非常廣泛的域的問題,但是我找不到解決這種常見問題的方法。我的問題特定於Visual Studio 2013,我對微軟開發環境來說真的很陌生。在Visual Studio 2013中進行REST API調用
我想用JSON響應做一個簡單的休息API調用。這也是迄今爲止它給我已經寫了代碼System.Security.SecurityException
:
private void btnAlert_Click(object sender, RoutedEventArgs e)
{
Console.Write("Event Fired");
MessageBox.Show("Event Fired");
String url = "http://www.traderscockpit.com/ModulusController?action=getInstruments";
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.BeginGetResponse(new AsyncCallback(FinishWebRequest), request);
try
{
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_downloaded);
client.DownloadStringAsync(new Uri(url));
}catch(Exception e1){
Console.Write(e1.ToString());
MessageBox.Show("Error Event Fired");
}
}
private void wc_downloaded(object sender, DownloadStringCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
string textString = (string)e.Result;
Console.Write(textString);
MessageBox.Show(textString);
}
else if(e.Cancelled){
Console.Write("canceled");
MessageBox.Show("cancelled");
}
else if (e.Error!=null)
{
Console.Write(e.Error.ToString());
MessageBox.Show(e.Error.ToString());
}
}
從這個
除此之外,一些代碼暗示此功能,VS 2013是不支持:
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
Response res=request.GetResponse();
有些人使用System.Net.Http;和System.Net.Http.Headers;命名空間,這再次不是在我的vs 2013年。
我真的很新,真的很震撼。提前致謝。
你所說的「是給'System.Security.Security'」意思?請注意,您正在使用的Visual Studio版本在執行時確實不會受到太多影響。知道你的目標是哪個版本的.NET以及哪個項目類型更重要。特別是,'System.Net.Http' * *是可用的,如果你有正確的程序集引用... – 2014-09-25 18:00:00
對不起System.Security.SecurityException。糾正它。 – Sourabh 2014-09-25 18:00:52
「不支持」你會得到錯誤嗎?我建議你避免使用'HttpWebRequest'並使用'HttpClient'來代替。例如'Install-Package Microsoft.Net.Http' – 2014-09-25 18:01:22