来自输入流的原始 XML 推送仅捕获 XML 的第一行

发布于 2024-10-08 05:28:49 字数 1961 浏览 8 评论 0原文

我正在尝试读取被推送到我的 java 应用程序的 XML。我最初在我的 glassfish 服务器中使用了这个。 glassfish 中的工作代码如下:

public class XMLPush implements Serializable
{    
public void processXML()
{
    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;
    try
    {
        br = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getReader ();
        String s = null;
        while((s = br.readLine ()) != null)
        {
            sb.append ( s );
        }
          //other code to process xml
        ...........
.............................

    }catch(Exception ex)
    {
        XMLCreator.exceptionOutput ( "processXML","Exception",ex);
    }
....
.....
}//processXML
}//class

它运行完美,但我的客户无法在其服务器上安装 glassfish。我尝试从 php 获取原始 xml,但无法让它工作。我决定打开一个套接字并手动监听 xml 推送。这是我接收推送的代码:

public class ListenerService extends Thread
{
private BufferedReader reader = null;
private String line;
public ListenerService ( Socket connection  )thows Exception
{
        this.reader = new BufferedReader ( new InputStreamReader ( connection.getInputStream () ) );
        this.line = null;

}//ListenerService
@Override
public void run ()
{
    try
    {
        while ( (this.line = this.reader.readLine ()) != null)
        {
            System.out.println ( this.line );
                 ........

        }//while
    }      System.out.println ( ex.toString () );
        }
    } catch ( Exception ex )
    {
        ...
    }//catch
}//run

我没有做过太多套接字编程,但从我过去一周读到的内容来看,将 xml 传递到字符串中是不好的。我做错了什么,为什么在 glassfish 服务器中它可以工作,而当我自己打开一个套接字时却不能?

这就是我从推送中收到的全部内容:

PUT /?XML_EXPORT_REASON=ResponseLoop&TIMESTAMP=1292559547 HTTP/1.1
Host: ************************
Accept: */*
Content-Length: 470346
Expect: 100-continue

<?xml version="1.0" encoding="UTF-8" ?>

xml 去了哪里?是因为我把它放在一个字符串中吗?我只需要获取 xml 并将其保存到文件中,然后对其进行处理。其他一切都有效,但这个。任何帮助将不胜感激。

I'm trying to read XML that is being pushed to my java app. I originally had this in my glassfish server working. The working code in glassfish is as follows:

public class XMLPush implements Serializable
{    
public void processXML()
{
    StringBuilder sb = new StringBuilder();
    BufferedReader br = null;
    try
    {
        br = ((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getReader ();
        String s = null;
        while((s = br.readLine ()) != null)
        {
            sb.append ( s );
        }
          //other code to process xml
        ...........
.............................

    }catch(Exception ex)
    {
        XMLCreator.exceptionOutput ( "processXML","Exception",ex);
    }
....
.....
}//processXML
}//class

It works perfect, but my client is unable to have glassfish on their server. I tried grabbing the raw xml from php, but I couldn't get it to work. I decided to open up a socket and listen for the xml push manually. Here is my code for receiving the push:

public class ListenerService extends Thread
{
private BufferedReader reader = null;
private String line;
public ListenerService ( Socket connection  )thows Exception
{
        this.reader = new BufferedReader ( new InputStreamReader ( connection.getInputStream () ) );
        this.line = null;

}//ListenerService
@Override
public void run ()
{
    try
    {
        while ( (this.line = this.reader.readLine ()) != null)
        {
            System.out.println ( this.line );
                 ........

        }//while
    }      System.out.println ( ex.toString () );
        }
    } catch ( Exception ex )
    {
        ...
    }//catch
}//run

I haven't done much socket programing, but from what I read for the past week is that passing the xml into a string is bad. What am I doing wrong and why is it that in glassfish server it works, and when I just open a socket myself it doesn't?

this is all that I receive from the push:

PUT /?XML_EXPORT_REASON=ResponseLoop&TIMESTAMP=1292559547 HTTP/1.1
Host: ************************
Accept: */*
Content-Length: 470346
Expect: 100-continue

<?xml version="1.0" encoding="UTF-8" ?>

Where did the xml go? Is it because I am placing it in a string? I just need to grab the xml and save it into a file and then process it. Everything else works, but this.Any help would be greatly appreciated.

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

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

发布评论

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

评论(2

岁月打碎记忆 2024-10-15 05:28:49

我认为这里的关键是推送请求中的这一行:

100-continue

来自 HTTP/1.1 规范:

100(继续)状态(请参阅第 10.1.1 节)的目的是允许发送带有请求正文的请求消息的客户端确定源服务器是否愿意接受该请求(基于请求标头)在客户端发送请求正文之前。在某些情况下,如果服务器在不查看正文的情况下拒绝消息,则客户端发送正文可能不合适或效率极低。

当您使用 GlassFish(或其他一些实现 HTTP 标准的技术)时,该中间件将处理所有协商,以确保当请求到达您的代码时,您拥有您能够接受的所有内容。因为在示例中您是直接从 Socket 读取数据,所以您和处理 HTTP 协商的客户端之间没有任何关系,因此您只得到前一点点和等待继续的“100-继续”。

I think the key here is this line in your push request:

100-continue

From the HTTP/1.1 specifications:

The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.

When you go through GlassFish (or some other technology which implements the HTTP standards), that middleware will handle all the negotiations to ensure that by the time the request gets to your code, you have everything you're able to accept. Because you're reading direct from the Socket in your example though, you have nothing between you and the client handling the HTTP negotiations, so you only get the first little bit and a '100-continue' waiting for the go-ahead.

一场春暖 2024-10-15 05:28:49

XML 的第一行有行终止符 (\r\n),但 XML 的其余部分没有。这就是我的线程卡住的原因。我使用

BufferedReader reader;
int i;
while((i = reader.read()) != -1)

它并将其转换为 char:

StringBuilder sb = new StringBuilder();
sb.append((char)i);

并将所有数据粘贴到我创建的 xml 文件中,并且能够正确执行所有操作。

唯一的问题是我在最后遇到了类似的问题,所以我必须检查是否到达了 XML 文档的末尾。我只是将流的最后 9 个字符放入数组列表中,并根据 XML 文件应以结尾的最后部分进行检查。现在工作正常,客户(还有我)非常高兴。 :) 希望这有帮助。

哦,最后一点。我读到 JDOM 实际上可以直接使用输入流。我还没有测试过它,但如果线路终止也不是问题的话,使用起来会更有效。也许其他人已经将 JDOM 与输入流一起使用并且遇到了行终止问题?

The first line of the XML had a line terminator (\r\n), but the rest of the XML did not. This is the reason why my threads would get stuck. I used

BufferedReader reader;
int i;
while((i = reader.read()) != -1)

and casted it into char:

StringBuilder sb = new StringBuilder();
sb.append((char)i);

and stuck all the data into an xml file I created and was able to do everything correctly.

The only thing is I had a similar problem at the end, so I had to check to see if I reached the end of the XML document. I just put the last 9 characters of the stream into an arraylist and checking it against the last part of what the XML file is supposed to end with. Works fine now, and the client (and I as well) is very happy. :) Hope this helps.

Oh one final note. I read that JDOM can actual use the input stream directly. I haven't tested it yet, but that would be more efficient to use, if the line termination will not be a problem either. Maybe someone else has used JDOM with inputstream and has had a line termination issue?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文