2017-10-24 2 views

답변

0

당신은 간단한 HttpURLConnection의에

샘플 코드 내용 본문으로 내용을 보낼 수 있습니다 :

URL endpoint = null; 

endpoint = new URL("https://someurlhere.com"); 

conn = (HttpURLConnection) endpoint.openConnection(); 

// Set the necessary header fields 

//The SOAPAction is required to be blank 
/*The presence and content of the SOAPAction header field can be used by servers such as firewalls to appropriately 
filter SOAP request messages in HTTP. The header field value of empty string ("") means that the intent of the SOAP 
message is provided by the HTTP Request-URI. No value means that there is no indication of the intent of the message.*/ 
conn.addRequestProperty("SOAPAction", ""); 
//Telling the server not to Skip the SOAPAction. It's a mandatory field when we send SOAP Requests. 
conn.addRequestProperty("Skip SOAP Action", "false"); 
conn.addRequestProperty("Content-Type", "text/xml;charset=UTF-8"); 

//We use POST because we are sending data to the server 
conn.setRequestMethod("POST"); 
//This is required to read response from Server 
conn.setDoOutput(true); 
//The server has 5 seconds to respond. Else, a timeout occurs and the connection is terminated. 
conn.setConnectTimeout(5000); 

// Send the request 
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
wr.write(xmlContent); 
wr.flush(); 


// Read the response 
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String temp; 

//Reading as long as there is response to read 
while ((temp = rd.readLine()) != null) { 
    response += temp; 
} 

을 보시다시피 xmlContent라는 문자열을 서버에 보내고 있습니다. 선택한 XML 파일로 내용을 설정하면 효과가 있습니다.

+0

실제로, 나는 wiremock으로 할 수있는 더 간단한 해결책을 찾고 있었지만, 역시 도움이 될 수 있습니다. 당신의 노력에 너무 감사합니다 :). –