Http客户端在java中发布xml文件 [英] Http client Post xml file in java

查看:153
本文介绍了Http客户端在java中发布xml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将xml文件发送到以下链接\

I need to send a xml file to the following link\

    http://14.140.66.142:80/MSMQ/private$/votes

这是我的代码。

   URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
    URLConnection con = url.openConnection();
    String document = "C:\\Documents and Settings\\Nagra\\My Documents\\Responseserver\\workingVoting\\VoteSubmitter\\Body.xml";

    FileReader fr = new FileReader(document);
    // specify that we will send output and accept input
    con.setDoInput(true);
    con.setDoOutput(true);
    char[] buffer = new char[1024*10];

    int b_read = 0;

    if ((b_read = fr.read(buffer)) != -1)

    {
        con.setRequestHeader ( "Content-Type", "text/xml" );
        con.setRequestProperty("SOAPAction","MSMQMessage");
        con.setRequestProperty("Proxy-Accept","NonInteractiveClient" );
        con.setRequestProperty("CONNECTION", "close");
        con.setRequestProperty("CACHE-CONTROL", "no-cache");
        con.setRequestProperty("USER-AGENT", "OpenTV-iAdsResponder_1_0");
        OutputStreamWriter writer = new OutputStreamWriter( con.getOutputStream() );
        writer.write(buffer, 0, b_read);
        PrintWriter pw = new PrintWriter(con.getOutputStream());
        pw.write(buffer, 0, b_read);
       pw.close();
        System.out.println("written");


  }
  catch( Throwable t )
{
    t.printStackTrace( System.out );
}

  }
  }

我不喜欢不知道它是否是正确的代码。如果我运行此代码,我无法在服务器端接收xml文件。任何人都可以帮助我在我的代码中出错的地方。

I don't Know whether it is right code.If i run this code I am not able to receive the xml file on the server side.Can anyone help me where i gone wrong in my code.

推荐答案

以下是POST操作示例:

Below is a sample POST operation:

URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("C:\\Documents and Settings\\Nagra\\My Documents\\Responseserver\\workingVoting\\VoteSubmitter\\Body.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

os.flush();
connection.getResponseCode();
connection.disconnect();

这篇关于Http客户端在java中发布xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆