【发布时间】:2014-08-04 20:01:00
【问题描述】:
为什么我只能在 .com 网址上发帖,而不能在 .asmx 网址上发帖?我有点困惑,因为我通常想做的是最终将 xml 内容发送到 .asmx url Web 服务。谁能给我提示为什么这不起作用,以及如何发布到 .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();
}
}
}
【问题讨论】:
-
.asmx不是顶级域。 -
@GriffeyDog 我需要做什么才能发布到 .asmx 网址,或将一串数据发送到 .asmx 网址?有什么技巧可以使用或继续做吗?
-
您需要知道正确的 URL 才能访问您尝试访问的任何 Web 服务。我所知道的是它不会采用您的示例显示的形式。
标签: java eclipse web-services asmx httpurlconnection