/// <summary> /// HttpWebResponse 처리 /// </summary> /// <param name="response"></param> private void responseProc(HttpWebResponse response) { Console.WriteLine("HttpRestClient Status: " + response.StatusCode + " - " + response.StatusDescription); ResponseCode = (int)response.StatusCode; ResponseMessage = response.StatusDescription; ContentType = response.ContentType; CharacterSet = response.CharacterSet; //Console.WriteLine("ContentType: " + response.ContentType); //Console.WriteLine("Charset: " + response.CharacterSet); using (MemoryStream memStream = new MemoryStream()) using (Stream respStream = response.GetResponseStream()) { respStream.CopyTo(memStream); ResponseContent = memStream.ToArray(); } // 복호화처리 //X-Encrypted 헤더 찾기 string encmode = response.GetResponseHeader("X-Encrypted"); if (UserEncryptMode && encmode != null && encmode.Equals("enabled")) { Console.WriteLine("Response data encrypted."); byte[] b64dec = Convert.FromBase64String(Encoding.UTF8.GetString(ResponseContent)); ResponseContent = AESUtil.Decrypt(b64dec); } }
/// <summary> /// HttpWebRequest 객체를 생성하고 초기화한다. /// </summary> /// <returns></returns> private HttpWebRequest createWebRequest() { makeUriString(); Console.WriteLine("HttpRestClient " + Method + " " + RequestUri); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(RequestUri); request.Method = Method; request.ReadWriteTimeout = Timeout * 1000; request.Timeout = Timeout * 1000; request.Accept = ResponseDataFormat; //request.Credentials = CredentialCache.DefaultCredentials; request.UserAgent = "StockItemData Client"; //request.Referer = ""; //request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip"); request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; // 인증정보가 다른 서버로 새어나가지 않도록 API 서버 주소와 비교한다. string urlBase = Configure.server + "/" + Configure.apiVersion; if (RequestUri.StartsWith(urlBase)) { // 인증헤더 추가 string authorize = getAuthorizationString(); if (authorize != null && authorize.Length > 0) { // 인식하지 않음 // request.Credentials 을 사용할 것을 권장하는데 개발하기 번거로우니, X- 헤더로 대체함 //request.PreAuthenticate = true; //request.Headers.Add("Authorization", authorize); request.Headers.Add("X-Authorization", authorize); } // 암호화 헤더 추가 if (UserEncryptMode) { // 암호화헤더 추가 byte[] key = AESUtil.createRandomKeys(AESUtil.AES_BITS_256); AESUtil.Key = key; string rsaEncodedKey = "AES/256/CBC," + RSAEncrypt.Encrypt(key); Console.WriteLine("X-Encrypt-Key:" + rsaEncodedKey); request.Headers.Add("X-Encrypt-Key", rsaEncodedKey); } } else { Console.WriteLine("요청 주소는 API서버가 아님 [" + RequestUri + "]"); // 암호화기능 disable UserEncryptMode = false; } return(request); }
/// <summary> /// 서버와 통신 /// </summary> public bool doWork() { if (isWorked) { return(isSuccess()); } isWorked = true; try { if (Configure.server.Length == 0) { ResponseMessage = "Configuration error!"; return(isSuccess()); } request = createWebRequest(); // POST or PUT if (Method.Equals(REST_METHOD_CREATE) || Method.Equals(REST_METHOD_UPDATE)) { // 암호화처리 if (RequestContent.Length > 0 && UserEncryptMode) { byte[] aesenc = AESUtil.Encrypt(RequestContent); RequestContent = Encoding.UTF8.GetBytes(Convert.ToBase64String(aesenc)); } request.ContentType = RequestDataFormat; //; charset=UTF-8 request.ContentLength = RequestContent.Length; if (RequestContent.Length > 0) { using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(RequestContent, 0, RequestContent.Length); } } } using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { ResponseCode = (int)WebExceptionStatus.Success; if (response != null) { responseProc(response); } else { Console.WriteLine("No response."); } } } catch (UriFormatException ufe) { Console.WriteLine("UriFormatException Message " + ufe.Message); Console.WriteLine("Uri : " + RequestUri); ResponseMessage = "UriFormatException: " + ufe.Message + " " + RequestUri; } catch (WebException we) { Console.WriteLine("WebException Message " + we.Message); if (we.Status == WebExceptionStatus.ProtocolError) { using (HttpWebResponse response = (HttpWebResponse)we.Response) { if (response != null) { responseProc(response); Exception basee = we.GetBaseException(); if (basee != null) { ResponseMessage += ": " + basee.Message; } else { ResponseMessage += ": " + we.Message; } } else { Console.WriteLine("No response."); ResponseMessage = we.Status.ToString() + ": " + we.Message; } } } else { ResponseCode = (int)we.Status; Exception basee = we.GetBaseException(); if (basee != null) { ResponseMessage = we.Status.ToString() + ": " + basee.Message; } else { ResponseMessage = we.Status.ToString() + ": " + we.Message; } } Console.WriteLine("RespCode " + ResponseCode); Console.WriteLine("RespMesg " + ResponseMessage); } return(isSuccess()); }