http - J2ME HttpConnection getResponseCode() 阻止来自诺基亚 N97 上服务器的等待应答

标签 http networking java-me nokia

我将文件从诺基亚 N97 手机上传到服务器,一切正常,但文件上传后我想从服务器获得响应。问题是我只在半分钟或更长时间后才收到回复。据我所见,httpConnection.getResponseCode() 中的代码块等待响应。我在 Sony Ericsson 上对其进行了测试,得到的响应非常快,因此我认为是诺基亚 N97 的问题。 (不是服务器问题,因为它在其他手机上工作正常)

有谁知道为什么这件事只发生在诺基亚?

这是一个代码片段:

公共(public)上传文件(){

       httpConn = (HttpsConnection) Connector.open(url, Connector.READ_WRITE);
        ...
       //set httpConn parameters and request method 
       ...

       writeParametersAndFileName(os, "text/plain");


       **writeFileToStream(os);** 

       os.write("\r\n".getBytes());
       os.write("--".getBytes());

       os.write(boundary.getBytes());

       os.write("--".getBytes());
       os.write("\r\n".getBytes());

        *//here code blocks on Nokia N97. Same thing happens if I use os.flush() after
        // I send chunks of the file*
         .....
        codeResp = httpConn.getResponseCode();
        // check condition
        checkResponseHeader();

公共(public) writeFileToStream() {

        InputStream is = null;
        FileConnection c = null;
        try
        {
           c = (FileConnection) Connector.open("file:///"+ sourcePath, Connector.READ);

           if(c.exists()) {
               is = c.openInputStream();

               long fs = c.fileSize();
               while (total < fs) {
                    byte[] data = new byte[512];
                    int readAmount = is.read(data,0,512);                       
                    total += readAmount;
                    os.write(data, 0, readAmount);


            }

            //os.flush();
        }
        catch (IOException ex) {
           //               
        }
        finally
        {
           //close connection

        }

最佳答案

你只认为你的文件已经上传。

您对 getResponseCode() 的调用是导致 HttpConnection 实际连接到服务器的第一个调用。

它在上传文件之前不会返回,这大概需要一分多钟。

根据 JSR-118 中的 HttpConnection 规范,这是完全有效的行为:

" 当连接处于设置状态时,以下方法会导致转换到连接状态。

* openInputStream
* openDataInputStream
* getLength
* getType
* getEncoding
* getHeaderField
* getResponseCode
* getResponseMessage
* getHeaderFieldInt
* getHeaderFieldDate
* getExpiration
* getDate
* getLastModified
* getHeaderField
* getHeaderFieldKey 

我预计您尝试过的其他手机不如 N97 强大,并且需要刷新 OutputStream,因此根据规范先连接到服务器。

关于http - J2ME HttpConnection getResponseCode() 阻止来自诺基亚 N97 上服务器的等待应答,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1872617/

相关文章:

c 程序未写入与显示相同的结果

networking - 是否可以使用Kubernetes Pod将硬件加入同一子网中?

c# - 如何检测 Windows 是否在 C# 中通过 LAN 或 WiFi 引导流量

android - 在手机上测量 "real"电话信号强度

button - 如何在 J2ME 应用程序的窗体上创建按钮?

java - 使用 json 发出 HTTP post 请求

c# - 在 C# 中使用 httpclienthandler 使用一个参数发布多个值

http - 如何从 Rust 发出 HTTP 请求?

java - 黑莓线程池工作不正常

http - Solr 通过 HTTP 上的游标将文档获取为 CSV