【问题标题】:Http client Post xml file in javaHttp客户端在java中发布xml文件
【发布时间】:2011-10-13 09:36:18
【问题描述】:

我需要发送一个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 文件。谁能帮助我在我的代码中出错的地方。

【问题讨论】:

    标签: java http post


    【解决方案1】:

    下面是一个示例 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();
    

    【讨论】:

    • 这是我在运行您的代码时遇到的错误。 org.xml.sax.SAXParseException:prolog 中不允许有内容。在 com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231) 在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java: 609) 在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:707) 在 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform( TransformerImpl.java:313) 在 hhtprequest.main(hhtprequest.java:52)
    • @bharathi - 您可以尝试在 HTTP 操作之外进行转换(将输出设置为 System.out)吗?此代码已为其他人成功:stackoverflow.com/questions/6665769/…
    【解决方案2】:

    您发布的代码存在几个问题。 首先,您只读取 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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-09
      • 2020-06-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2011-10-18
      • 1970-01-01
      相关资源
      最近更新 更多