기술참고자료/JAVA | 2011. 3. 17. 16:37
How to send an XML document to a remote web server using HTTP POST
HTTP POST를 이용하여 원격지의 웹서버로 XML문서를 전송하는 방법에 대한 참고자료
String strURL = "http://웹서버의경로";
String strXMLFilename = "c:/input.xml";
File input = new File(strXMLFilename);
PostMethod post = new PostMethod(strURL);
try {
post.setRequestEntity(
new InputStreamRequestEntity(
new FileInputStream(input), input.length()));
post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
HttpClient httpClient = new HttpClient();
int result = httpClient.executeMethod(post);
System.out.println("Response status code : " + result);
System.out.println("Response body : ");
System.out.println(post.getResponseBodyAsString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
파일형태로 저장된 XML 은 위의 코드처럼 하면 되는데,
내가 필요한건 문자열로 구성된 XML데이터의 전송이였다.
아래의 코드처럼 StringBuffer 클래스와 ByteArrayInputStream 클래스를 이용하여 이를 해결해보았다.String strURL = "웹서버의 경로";
StringBuffer strInputXMLBuffer = new StringBuffer();
strInputXMLBuffer.append("<FortuneInput>");
strInputXMLBuffer.append("<mentorType>" + 0 + "</mentorType>");
strInputXMLBuffer.append("</FortuneInput>");PostMethod post = new PostMethod(strURL);
try {
InputStream inputStream =
new ByteArrayInputStream(strInputXMLBuffer.toString().getBytes("UTF-8"));
post.setRequestEntity(
new InputStreamRequestEntity(inputStream, strInputXMLBuffer.length()));
post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
HttpClient httpClient = new HttpClient();
int result = httpClient.executeMethod(post);
System.out.println("Response status code : " + result);
System.out.println("Response body : ");
System.out.println(post.getResponseBodyAsString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
post.releaseConnection();
}
참고자료 : http://foldurl.com/206706
[JAVA] XML 직렬화도구 - Simple Framework (1) | 2011.06.21 |
---|---|
[JAVA] Hashmap & List 정렬기법 (0) | 2011.05.02 |
HttpClient의 심플한 사용 (0) | 2011.03.21 |
List Sort Sample Using Comparator<T> Interface (0) | 2011.03.17 |
DOM Parser vs SAX Parser (0) | 2011.03.17 |
Recent Comments