이곳 저곳 돌아다니면서 카페, 블로그를 찾아봤는데 도저히 못찾는 이 답답함....
결국 많은 테스트를 하면서 해답을 얻었다는...

일단 가장 손쉬운... 누구나 조금만 검색해보면 찾을수 있는 방법은

 HttpClient 와 PostMethod 를 이용한 방법
 파일 전송은 불가능하다.(혹,,, 가능하시면 댓글좀 ^_^ 굽신)

HttpClient client = null;
PostMethod method = null;

Protocol pt=null;       
if(protocolInfo.equals("http")){
    pt=new Protocol("http",new DefaultProtocolSocketFactory(),targetPort);
}else if(protocolInfo.equals("https")){
    pt=new Protocol("https",new SSLProtocolSocketFactory(),targetPort);
    Protocol.registerProtocol("https",pt);
}

HostConfiguration host=null;
host    = new HostConfiguration();
client  = new HttpClient();
method  = new PostMethod();

host.setHost(ip, "/TEST", targetPort, pt);
method.setPath("/TEST/Login");

method.addParameter("id", id);
method.addParameter("passwd", passwd);

int status = client.executeMethod(host, method);
if(status == HttpStatus.SC_OK) {
    br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
    while((line = br.readLine()) != null) {
        tmpBuffer.append(line);
    }
}else {
    return "FAIL";
}

흠.... 뭐 이정도?


이런 소스야 어딜가든 구할수 있는 소스다.
한글도 UTF-8로 전송도 잘되고...

하지만 가장 문제는 파일까지 전송 할경우 어떻게 해야 하는가? 인데...
저런식으로 별의 별 짓을 다해봐도 한글은...쫑!!! 줴길.... 어쩌란 말인가??

몇날 몇일을 삽질과 함께 하고 결국 찾은 해답... 안. 된. 다... 라고 하면 난 용자^_^ ㅋㅋㅋ.

 HttpClient 와 MultipartPostMethod를 이용한 방법
 파일 전송도 가능하다. 단, 설정 해야 하는게 좀 불편 하다 랄까?

File file = null;
file = new File("file_path",sendFileName);

NameValuePair strPair1 =  new NameValuePair("id",id);
NameValuePair strPair2 =  new NameValuePair("passwd",passwd);
NameValuePair strPair3 =  new NameValuePair("file_data",sendFileData);

MultipartPostMethod mMulti  = new MultipartPostMethod();
mMulti.addParameter("file_name",file);
mMulti.setQueryString(new NameValuePair[]{strPair1,strPair2,strPair3,strPair4,strPair5,strPair6,strPair7,strPair8,strPair9,strPair10});

HttpClient client = null;
MultipartPostMethod mMulti = multipostmethod;
Protocol pt=null;       

if(protocolInfo.equals("http")){
    pt=new Protocol("http",new DefaultProtocolSocketFactory(),targetPort);
}else if(protocolInfo.equals("https")){
    pt=new Protocol("https",new SSLProtocolSocketFactory(),targetPort);
    Protocol.registerProtocol("https",pt);
}
HostConfiguration host=null;
host    = new HostConfiguration();
client  = new HttpClient();

mMulti.setRequestHeader("Content-Type", "multipart/form-data; charset=UTF-8");
String strPath = "/TEST/Login";
host.setHost(ip, "/TEST", targetPort, pt);
mMulti.setPath(strPath);

BufferedReader br = null;
String line = "";
StringBuffer tmpBuffer = new StringBuffer();
int status = client.executeMethod(host, mMulti);
if(status == HttpStatus.SC_OK) {
    br = new BufferedReader(new InputStreamReader(mMulti.getResponseBodyAsStream()));
    while((line = br.readLine()) != null) {
        tmpBuffer.append(line);
    }
}
else {
    return "FAIL";
}
뭐.. 이정도면 샘플 정도 가능할꺼라 생각됨..+_+


이렇게 사용하면 일단 데이터 보내는 작업은 완료 된 샘이다.

그럼 받는쪽은....아....이거까지 적기에는 나의 귀차니즘이 폭발 직전이라..

그런건 너~~~무 많이 널려있고 조금만 검색하면 나오니깐 다른분들에게 그 작업을 돌립니다.-_-;;;

혹시 이글 퍼 가실때는 출처를 확실하게 해주세요.

'Programer > JAVA/C#' 카테고리의 다른 글

jdbc - mssql 인스턴스 연결  (0) 2011.12.26
java yyyyMMddHHmmss 이상의 date  (0) 2011.12.09
PreparedStatement로 2000byte를 못넣는다?  (0) 2010.01.20
PocketPC를 이용한 웹서비스  (0) 2010.01.06
JAVA 한글 깨짐  (0) 2010.01.06