公司购买的百傲瑞达一卡通软件,提供Restful API调用,使用SoapUI能够调用成功,但在C#里用代码调用时一直报错:"远程服务器返回错误: (500) 内部服务器错误"
找了很久,最后发现是需要设置UserAgent,在SoapUI里面找到成功调用时显示的User-Agent,再在代码里加上一句给Request赋UserAgent的语句即可。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.UserAgent = "Apache-HttpClient/4.1.1 (java 1.5)";
request.ContentType = "application/json;charset=UTF-8";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();