Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class WebAPIRequester
- {
- public WebAPIRequester() { }
- private string requestUrlString;
- private string authorizationString;
- private string contentType;
- private string acceptType;
- public void Connect(string requestUrlString, string authorizationString, string contentType, string acceptType)
- {
- this.requestUrlString = requestUrlString;
- this.authorizationString = authorizationString;
- this.contentType = contentType;
- this.acceptType = acceptType;
- }
- public string GetData(string postData)
- {
- var request = (HttpWebRequest)WebRequest.Create(requestUrlString);
- var data = Encoding.UTF8.GetBytes(postData);
- request.Method = "POST";
- request.PreAuthenticate = true;
- request.Headers.Add("Authorization", this.authorizationString);
- request.ContentType = this.contentType;
- request.Accept = this.acceptType;
- request.ContentLength = data.Length;
- using (var stream = request.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- var response = (HttpWebResponse)request.GetResponse();
- var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
- return responseString;
- }
- public string GetData<T>(T operation)
- {
- return GetData(FormatPostData(operation));
- }
- private string FormatPostData<T>(T operation)
- {
- var serializer = new XmlSerializer(typeof(T));
- string result;
- using (var sww = new StringWriter())
- {
- using (var writer = XmlWriter.Create(sww))
- {
- serializer.Serialize(writer, operation, new XmlSerializerNamespaces());
- result = sww.ToString();
- }
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement