我试图使用HttpWebRequest从服务器获取JSON。但是,尝试使用不同JSON Im的不同网址总是会“请求超时”。这是我的代码:
//url = "http://46.105.85.199:3000/api/books
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = WebRequestMethods.Http.Get;
request.Accept = "application/json";
request.ContentType = "application/json";
request.Timeout = 300000;
var response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string output = reader.ReadToEnd();
response.Close();
return output;`
和JSON:
[
{
"metaData": {
"title": "Гарри Поттер и Принц-полукровка (Росмэн) -0",
"language": "ru",
"date": "2005",
"creator": "Джоан Кэтлин Роулинг",
"creatorFileAs": "Джоан Кэтлин Роулинг",
"publisher": "РОСМЭН",
"description": "",
"subject": "child_adv"
},
"chapters": [
{
"id": "cover",
"href": "OEBPS/cover.xhtml",
"media-type": "application/xhtml+xml"
},
...
],
"name": "legion.epub",
"url": "/upload/common/legion.epub",
"lastMark": null,
"cover": "./client/upload/images/missing.jpg",
"id": 8,
"readerId": null,
"teamId": null
}
]
等等。我也尝试过id和章节的书,但结果相同。
答案 0 :(得分:1)
为了缩小问题范围,首先向google.com等第三方服务器发送一个简单的get请求,但未指定Accept
或ContentType
。
如果超时未重现,则可能是服务器上的问题隐藏在url
之后。如果超时仍然存在,则可能是客户端(设备/模拟器)或Web服务器/ wifi路由器/等之间存在问题。
为了检查后者,请从您的PC(即标准浏览器)发出相同的请求。它是否快速检索响应?