출처-Http 송수신 HttpClient
POST 방식 예제
01 |
import org.apache.commons.httpclient.HttpClient; |
02 |
import org.apache.commons.httpclient.HttpStatus; |
03 |
import org.apache.commons.httpclient.methods.PostMethod; |
05 |
import java.io.BufferedReader; |
06 |
import java.io.InputStreamReader; |
08 |
public class PostMethodExample { |
09 |
public static void main(String args[]) { |
10 |
HttpClient client = new HttpClient(); |
11 |
client.getParams().setParameter( "http.useragent" , "Test Client" ); |
12 |
BufferedReader br = null ; |
14 |
method.addParameter( "p" , "\"java2s\"" ); |
16 |
int returnCode = client.executeMethod(method); |
18 |
if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) { |
19 |
System.err.println( "The Post method is not implemented by this URI" ); |
21 |
method.getResponseBodyAsString(); |
23 |
br = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream())); |
25 |
while (((readLine = br.readLine()) != null )) { |
26 |
System.err.println(readLine); |
29 |
} catch (Exception e) { |
30 |
System.err.println(e); |
32 |
method.releaseConnection(); |
33 |
if (br != null ) try { br.close(); } catch (Exception fe) {} |
결과 받는 다른 예제)
byte[] responseBody = method.getResponseBody();
method.getRequestEntity();
String xml = new String(responseBody);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new StringReader(xml));
Element root = doc.getRootElement();
String ResponseValue = root.getChild("ResponseValue").getValue();