简体   繁体   English

使用httpurlconnection发布到Web服务

[英]Posting to a webservice using httpurlconnection

How come I am only allowed to make posts to .com url's but not .asmx url's? 为什么只允许将帖子发布到.com网址,却不允许发布到.asmx网址? Im a bit confused as what I want to generally do is send xml content to a .asmx url web service eventually. 我有点困惑,因为我通常想做的是最终将xml内容发送到.asmx网址Web服务。 Can anyone supply me with tips why this doesn't work, and how I can post to a .asmx file? 谁能为我提供提示,为什么它不起作用以及如何发布到.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();
        }
    }

}

Using "POST" is correct. 使用“ POST”是正确的。

Instead of calling connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 而不是调用connection.setRequestProperty(“ Content-Type”,“ application / x-www-form-urlencoded”); you have to call connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 您必须调用connection.setRequestProperty(“ Content-Type”,“ text / xml; charset = utf-8”); (if you are using utf-8 encoding which is probably the case). (如果您使用的是utf-8编码,则可能是这种情况)。

You also have to set the SOAP Action in the http- Header: connection.setRequestProperty("SOAPAction", SOAPAction); 您还必须在http- Header中设置SOAP Action:connection.setRequestProperty(“ SOAPAction”,SOAPAction); You can find the SOAP Action eihter in the wsdl- file. 您可以在wsdl-文件中找到SOAP Action标识符。 What I did to find out all expected Parameters: I used a working WS Client, and traced the TCP traffic in order to find out the expected HTTP headers. 我做了什么来找出所有预期的参数:我使用了一个正常工作的WS客户端,并跟踪了TCP流量以找出预期的HTTP标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM