-
Notifications
You must be signed in to change notification settings - Fork 10
/
ApiRequest.cs
54 lines (45 loc) · 1.65 KB
/
ApiRequest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace ApiCore
{
public static class ApiRequest
{
private static HttpWebRequest request;
private static HttpWebResponse response;
public static int Timeout = 15000;
/// <summary>
/// Send request to API
/// </summary>
/// <param name="url">Url to request</param>
/// <returns>String, contains response from api. by default, it's - XML</returns>
public static string Send(string url)
{
try
{
ApiRequest.request = (HttpWebRequest)HttpWebRequest.Create(url);
ApiRequest.request.UserAgent = "xternalx vkontakte api wrapper/0.1a";
ApiRequest.request.Timeout = ApiRequest.Timeout;
ApiRequest.request.Method = "GET";
ApiRequest.response = (HttpWebResponse)ApiRequest.request.GetResponse();
Stream responseStream = ApiRequest.response.GetResponseStream();
if (responseStream == null)
{
throw new ApiRequestNullResult("No content was been downloaded");
}
else
{
StreamReader sr = new StreamReader(responseStream);
return sr.ReadToEnd();
}
}
catch(Exception e)
{
throw new ApiRequestNullResult("Unknown result: "+e.Message);
}
}
}
}