public void Run(ResponeCode code, FileInfo file, bool fileoption = false) { try { HeaderOption option = HeaderOption.Create(); if (fileoption) { option.AddOption("Content-Type", "multipart/formed-data"); option.AddOption("Content-Disposition", "attachment; filename=" + file.Name); option.AddOption("Length", file.Length.ToString()); } else { option.AddOption("Content-Type", "text/html"); } byte[] header = BuildHeader(code, option); client.Send(header); if (file != null && file.Exists) { byte[] data = GetFile(file); client.Send(data); } } catch (Exception e) { LastException = e; client.Send(BuildHeader(ResponeCode.CODE500)); } finally { client.Close(); } }
private byte[] BuildHeader(ResponeCode code, HeaderOption option = null) { String2 ret = new String2(Encoding.UTF8); if (object.Equals(code, ResponeCode.CODE200)) { ret += "HTTP/1.1 200 OK" + String2.CRLF; } else { ret += "HTTP/1.1 " + ((int)code) + " NG" + String2.CRLF; } if (option != null) { foreach (String key in option.ToResult().Keys) { ret += key + ": " + option.ToResult()[key] + String2.CRLF; } } ret += "Keep-Alive: timeout=15, max=93" + String2.CRLF; ret += "Connection: Keep-Alive" + String2.CRLF + String2.CRLF; return(ret.ToBytes()); }