검색결과 리스트
글
WebClient 이용해서 일단 데이터 날리는 방법과 파일 업/다운로드 방식이다.
귀찮아서 창고용으로 하핫~
물론 데이터 전송과 파일 업로드는 웹 구현이 필요하다.
[데이터 전송]
Encoding encode = System.Text.Encoding.GetEncoding("euc-kr");
byte[] result = encode.GetBytes(웹으로 날릴 인자 값);
Uri uri = new Uri(처리 해 줄 웹);
HttpWebRequest wReqFirst = (HttpWebRequest)WebRequest.Create(uri);
wReqFirst.Method = "POST";
wReqFirst.Accept = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.5.20706; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
wReqFirst.Method = "POST";
wReqFirst.UserAgent = "웹에 설정한 인증자";
wReqFirst.Headers.Add("trans-user", "");
wReqFirst.ContentType = "application/x-www-form-urlencoded";
wReqFirst.ContentLength = result.Length;
wReqFirst.Headers.Add("trans-key", "");
wReqFirst.Headers.Add("Cookie: 세션="세션값";");//요건 선택 사항 하고 싶은 사람은 하면 됨 ``/ 세션 유지 할 때 이용시 유용 한 느낌~
Stream postDataStream = wReqFirst.GetRequestStream();
postDataStream.Write(result, 0, result.Length);
postDataStream.Close();
HttpWebResponse wRespFirst = (HttpWebResponse)wReqFirst.GetResponse();
Stream respPostStream = wRespFirst.GetResponseStream();
StreamReader readerPost = new StreamReader(respPostStream, encode);
string resultPost = readerPost.ReadToEnd();
[파일 업로드]
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.Accept = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.5.20706; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
webrequest.UserAgent = "웹에 설정한 인증자";
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
헤더 값에 업로드 할 파일 명 등 추가~ 웹에서 구현하기 나름이다.
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse responce = webrequest.GetResponse();
Stream s = responce.GetResponseStream();
[파일 다운로드]
WebClient webclient = new WebClient();
Uri uri = new Uri(서버 주소 + 다운로드 할 파일 명);
webclient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.5.20706; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;)");
webclient.Credentials = new NetworkCredential("sitemgr", "S2_.T8UJ");
webclient.UseDefaultCredentials = true;
webclient.DownloadFileAsync(uri, 올릴 파일 전체 경로);
while (webclient.IsBusy) { System.Windows.Forms.Application.DoEvents(); System.Threading.Thread.Sleep(1); };
뭐.. 대략 이 정도 인듯?
창고는 쌓아두는게 제맛!!
제때 제때 잘 찾음 MSDN 부럽지 않다 ㅋㅋ
설정
트랙백
댓글
글
여지껏 배포하고 했을 때
stdole.dll 이란 파일은 한번도 넣은 적이 없다.
근데 이번에 프로젝트 하면서..
저녀석이 없으니...
컴포넌트부터 해서 말썽이네? 흠냐;;
뭐 무튼 stdole.dll 명심해두기.
설정
트랙백
댓글
글
만들어 놓은걸 귀찮게 왔다 갔다 하기 싫어서..
포스팅을 하긴 하는데.. 흠.. 심플하다 역시. 모두들 다 아는 방법 일 것이고
1. Upload
Uri ftpUri = new Uri("ftp경로 + 파일 이름");
FtpWebRequest reqFtp = (FtpWebRequest)WebRequest.Create(ftpUri);
reqFtp.Credentials = new NetworkCredential("id", "password");
reqFtp.UseBinary = true;
reqFtp.UsePassive = true;
reqFtp.KeepAlive = false;
reqFtp.Timeout = 10000;
reqFtp.Method = WebRequestMethods.Ftp.UploadFile;
FileInfo fileinfo = new FileInfo(@"C:\Result110830.txt");
FileStream fstream = fileinfo.OpenRead();
byte[] buffer = new byte[2048];
int curroffset = 0; try
{
Stream stream = reqFtp.GetRequestStream();
curroffset = fstream.Read(buffer, 0, 2048);
while (curroffset != 0)
{
stream.Write(buffer, 0, curroffset);
curroffset = fstream.Read(buffer, 0, 2048);
}
}
catch (Exception ex)
{
}
2. Download
Uri ftpUri = new Uri("ftp경로 + 파일 이름");
FtpWebRequest reqFtp = (FtpWebRequest)WebRequest.Create(ftpUri);
reqFtp.Credentials = new NetworkCredential("id", "password");
reqFtp.Timeout = 10000;
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse resFtp = (FtpWebResponse)reqFtp.GetResponse();
StreamReader reader = new StreamReader(resFtp.GetResponseStream());
string strData = reader.ReadToEnd();
//ftp 경로 내의 파일 정보 읽어 오는 부분
string[] filesInDirectory = strData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
resFtp.Close();
try
{
foreach (string _str in filesInDirectory)//파일명
{
//이미 윗 부분에서 권한을 가져 왔기 때문에 다시 권한을 지정 해 줄 필요가 없다.
WebClient wclient = new WebClient();
wclient.Credentials = new NetworkCredential();
wclient.DownloadFileAsync(ftpUri, @"C:\dsneat\" + _str);
}
}
catch (Exception exx)
{
}
두가지 방법으로 왔다 갔다 하면 그냥 왠만한건 다 되는거 같다.
기억상에 폴더 채로 해 봤던 적이 있었던거 같은데.. 기억이 잘 안난다 ㅠ_ㅠ
누가 알면.. 간단히 리뷰 해 주면 좋겠는데 ㅠ_ㅠ
그럼 오늘도 열심히 화이팅!!
RECENT COMMENT