Created
February 22, 2022 08:29
-
-
Save peymanmajidi/9c64dddcf856b6aedfe20ce1c90abae9 to your computer and use it in GitHub Desktop.
Easy Rest API With Serialize and Deserialize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool EasyRest(long sortingDataId, out string error, out int error_code, out bool fatal) | |
{ | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create(Api.ApiOptions.LightSortingConfirmationURL); | |
httpWebRequest.ContentType = "application/json"; | |
httpWebRequest.Method = "POST"; | |
httpWebRequest.Headers.Add("Authorization", $"{ApiOptions.TokenType} {ApiOptions.Token}"); | |
error_code = 0; | |
fatal = false; | |
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) | |
{ | |
string json = JsonConvert.SerializeObject(new | |
{ | |
sortingDataId = sortingDataId | |
} | |
); | |
streamWriter.Write(json); | |
} | |
try | |
{ | |
using (var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse()) | |
{ | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
var result = streamReader.ReadToEnd(); | |
var json_result = JsonConvert.DeserializeObject<IGTResponse>(result); | |
if (json_result != null) | |
{ | |
error = json_result.errorMessage; | |
error_code = json_result.errorCode; | |
return true; | |
} | |
} | |
} | |
error = "Return result is not valid"; | |
return false; | |
} | |
catch (WebException ex) | |
{ | |
error = ex.Message; | |
fatal = true; | |
if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null) | |
{ | |
var resp = (HttpWebResponse)ex.Response; | |
if (resp.StatusCode == HttpStatusCode.NotFound) | |
{ | |
error = "404 Page Not Found, URL is not valid"; | |
return false; | |
} | |
} | |
} | |
return false; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment