Почему мне разрешено делать сообщения только на.com url, но не на.asmx url? Я немного смущен, так как, как я обычно хочу, это отправить xml-контент в веб-службу.asmx url в конце концов. Может ли кто-нибудь дать мне советы, почему это не работает, и как я могу опубликовать файл.asmx?
public class POSTSenderExample {
public String echoCuties(String query) throws IOException {
// Encode the query
String encodedQuery = URLEncoder.encode(query, "UTF-8");
// This is the data that is going to be send to itcuties.com via POST request
// 'e' parameter contains data to echo
String postData = "e=" + encodedQuery;
URL url = new URL("http://echo.itgeeeks.asmx");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postData.length()));
// Write data
OutputStream os = connection.getOutputStream();
os.write(postData.getBytes());
// Read response
StringBuilder responseSB = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ( (line = br.readLine()) != null)
responseSB.append(line);
// Close streams
br.close();
os.close();
return responseSB.toString();
}
// Run this example
public static void main(String[] args) {
try {
System.out.println(new POSTSenderExample().echoCuties("Hi there!"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Использование "POST" является правильным.
Вместо вызова connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); вам нужно вызвать connection.setRequestProperty("Content-Type", "text/xml; charset = utf-8"); (если вы используете кодировку utf-8, которая, вероятно, имеет место).
Вы также должны установить действие SOAP в заголовке http-: connection.setRequestProperty("SOAPAction", SOAPAction); Вы можете найти действие SOAP Action eihter в файле wsdl-. Что я сделал, чтобы узнать все ожидаемые параметры: я использовал рабочий WS-клиент и отслеживал трафик TCP, чтобы узнать ожидаемые заголовки HTTP.
.asmx
не является доменом верхнего уровня.