How can I write an api post request from Java to c#

时间:2016-05-11 11:32:25

标签: java c#

Recently I needed to move some code from Java to C#.

This is one of the methods that I couldn't rewrite... can someone help me?

public static String coreApiPostRequest(URL url, String input) {

    try {

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/xml");
        String responseMessage=null;


        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        os.flush();


        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        while ((output = br.readLine()) != null) {
            System.out.println(output);
            responseMessage = output;
        }

        conn.disconnect();
        return responseMessage;

      } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;

      } catch (IOException e) {
        e.printStackTrace();
        return null;
     }
}

0 个答案:

没有答案
相关问题