-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathWebService.cs
95 lines (88 loc) · 3.46 KB
/
WebService.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
//using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;
//using System.Data.SqlServerCe;
using System.Data.Common;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;
using System.IO;
namespace DataSourceServices
{
class WebService : DataSource
{
//string siteUrl = "http://nomad.host22.com/";
string siteUrl = "http://localhost/";
public override byte[] GetImage(IMAGE_TYPE imageType, int id)
{
String url;
if (imageType == IMAGE_TYPE.picture)
url = siteUrl + "kuwaitindex/bio_picture.php?id=";
else
url = siteUrl + "kuwaitindex/bio_wsq.php?id=";
url += id.ToString();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
byte[] bytes = null;
using (Stream sm = request.GetResponse().GetResponseStream())
{
try
{
//List<JsonResult> result = jsonStr.FromJson<List<JsonResult>>(s);
//StreamReader sr = new StreamReader(sm);
//String str = sr.ReadToEnd();
//sr.Close();
DataContractJsonSerializer serialiser = new DataContractJsonSerializer(typeof(List<JsonResult>));
List<JsonResult> result = serialiser.ReadObject(sm) as List<JsonResult>;
if (result.Count != 0)
{
if (result[0].result != null && result[0].result != "success")
throw new Exception(result[0].result);
//MessageBox.Show(result[0].result);
else
{
try
{
if (imageType == IMAGE_TYPE.picture)
{
if (result[0].picture != null)
bytes = System.Convert.FromBase64String(result[0].picture);
}
else
{
if (result[0].wsq != null)
bytes = System.Convert.FromBase64String(result[0].wsq);
}
}
catch (Exception ex) { throw new Exception(ex.Message); }
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
return bytes;
}
}
[DataContract]
class JsonResult
{
#pragma warning disable 0649 //warning CS0649: Field 'DataSourceServices.JsonResult.result' is never assigned to, and will always have its default value null
[DataMember(Name = "result", IsRequired = false)]
public string result;
[DataMember(Name = "picture", IsRequired = false)]
public string picture;
[DataMember(Name = "wsq", IsRequired = false)]
public string wsq;
#pragma warning restore 0649
}
}