Http客户端在java中Post xml文件

发布于 2024-12-09 08:55:32 字数 1473 浏览 0 评论 0原文

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

    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 need to send a xml file to the following link\

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

This is my code.

   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 );
}

  }
  }

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆梦 2024-12-16 08:55:32

下面是一个 POST 操作示例:

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();

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();
音盲 2024-12-16 08:55:32

您发布的代码存在一些问题。
首先,您仅读取 1024*10 个字符,如果文件有更多字符,则不会发送整个文件。其次,您多次编写内容。将代码更改为与此类似的内容。

URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
HttpURLConnection con = (HttpURLConnection)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;
con.setRequestProperty ( "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() );
while ((b_read = fr.read(buffer)) != -1) {
    writer.write(buffer, 0, b_read);
}
writer.flush();
writer.close();
fr.close();
int i = con.getResponseCode();
con.disconnect();
System.out.println(String.format("written with response code: %d",i));

There are a couple of issues with the code you have posted.
First, you are reading only 1024*10 characters and you are not sending the whole file if the file has more characters. Second, you are writing the content more than once. Change the code something similar to this.

URL url = new URL("http://14.140.66.142:80/MSMQ/private$/votes");
HttpURLConnection con = (HttpURLConnection)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;
con.setRequestProperty ( "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() );
while ((b_read = fr.read(buffer)) != -1) {
    writer.write(buffer, 0, b_read);
}
writer.flush();
writer.close();
fr.close();
int i = con.getResponseCode();
con.disconnect();
System.out.println(String.format("written with response code: %d",i));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文