-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallwebservice.cs
74 lines (70 loc) · 2.67 KB
/
callwebservice.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace CameraAppDemo
{
public static class FetchAsync
{
public static async Task<string> CallWebService(string verb, string url, string requestBody = "")
{
string jsonDoc = "";
// Create an HTTP web request using the URL:
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.Method = verb;
if (requestBody != "")
{
request.ContentType = "application/json";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(requestBody);
request.ContentLength = data.Length;
Stream myReqStream = null;
try
{
myReqStream = request.GetRequestStream();
myReqStream.Write(data, 0, data.Length);
myReqStream.Close();
}
catch (Exception e)
{
Console.Write(e.Message);
}
}
try
{
using (WebResponse response = await request.GetResponseAsync())
{
if (response.ContentLength > 0)
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
jsonDoc = await Task.Run(() =>
{
byte[] bytes = new byte[response.ContentLength + 10];
int numBytesToRead = (int)response.ContentLength;
int numBytesRead = 0;
do
{
// Read may return anything from 0 to 10.
int n = stream.Read(bytes, numBytesRead, 10);
numBytesRead += n;
numBytesToRead -= n;
} while (numBytesToRead > 0);
return System.Text.Encoding.Default.GetString(bytes);
});
}
}
}
}
catch (WebException e)
{
Console.Write(e.Message);
}
return jsonDoc;
}
}
}