如何检查HttpWebResponse给出状态代码200?

时间:2018-06-04 14:28:07

标签: c# httpwebresponse

即时通讯使用一个api有些api给出了禁止错误403.如何检查webrequest的状态代码

错误 System.Net.WebException:'远程服务器返回错误:(403)Forbidden。'

var assetTracker = new AssetTracker { deviceid = deviceID };
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
req.Method = "GET";
req.Headers["Authorization"] = "Basic " + "QzdBQ";
req.ContentType = "application/vnd.onem2m-res+json";
req.Accept = "application/vnd.onem2m-res+json;";
req.Headers["Cache-Control"] = "no-cache";
req.Headers["X-M2M-RI"] = "99000";
req.Headers["X-M2M-Origin"] = "C7AAC";
// Here I need to check the status code

HttpWebResponse resp = req.GetResponse() as HttpWebResponse;//getting error

0 个答案:

没有答案