用户代理和Blackberry 6.0?

时间:2011-03-30 10:14:23

标签: blackberry

在我的Blackberry OS 6.0应用程序中,我使用HttpConnection发布多个图像文件, 这是我正在尝试的,

        byte[] _dataToBePost = strPostData.getBytes();
        String lineEnd = "\r\n";
        String boundary = "----------------------------";   

        String boundaryStartBytes = "------------------------------\r\n";
        byte[] startBytes = boundaryStartBytes.getBytes();

        String boundaryEndBytes = "\r\n------------------------------\r\n";
        byte[] endBytes = boundaryEndBytes.getBytes();  
        _httpConnection = (HttpConnection)Connector.open(url,Connector.READ_WRITE,true); 

            // Set the request method and headers
            _httpConnection.setRequestMethod(HttpConnection.POST);
            _httpConnection.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT");
            _httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
            _httpConnection.setRequestProperty("Content-Language", "en-US");



 if( PhotoToSend != null)
            //if(AttachPhotos._vctAccPhotos.size() > 0)
            {
                String[] strAccidentPhoto = {"AccidentPhoto1", "AccidentPhoto2", "AccidentPhoto3", "AccidentPhoto4", "AccidentPhoto5"};         

                for(int i=0; i<5; i++)
                {
                    String header = "Content-Disposition: form-data; name=\"file1\";filename=\""+ "AccidentPhoto"+ i +".jpg"+ "\"" + lineEnd + "Content-Type: application/octet-stream"+lineEnd+lineEnd;
                    byte[] composition = header.getBytes();
                    byte[] photoData = AttachPhotos.get(strAccidentPhoto[i]);
                    if(photoData != null)
                    { 
                        _outputStream.write(startBytes);
                        _outputStream.write(composition);
                        _outputStream.write(photoData);
                        _outputStream.write(endBytes);
                    }
                }
            }

在我的代码中我使用User-Agent作为Profile / MIDP-2.0 Configuration / CLDC-1.0。发布多个文件会产生任何问题吗?或者是否有其他方式发布数据。 代码不会通过任何异常,但只能发布图像文件。 我的代码中缺少什么?

1 个答案:

答案 0 :(得分:0)

  

在我的代码中我使用User-Agent作为   资料/ MIDP-2.0   配置/ CLDC-1.0。这是在制作   发布多个文件的任何问题?

没有。 我还建议不要触摸此标题,因为预计操作系统会以最恰当的方式设置它。

更新:感谢gnuf​​ - 我说“操作系统会以最恰当的方式设置它”是不正确的。事实证明,除非你设置它,否则根本就没有这样的标题。但是,我不认为这可能是一个原因,除非您指示服务器拒绝请求,如果他们没有User-Agent标头(这不太可能)。

  

还是有其他方式发布   数据。

可能有多种原因。基本上你需要的是发送'multipart / form-data'类型的POST请求。所以你需要对应这种请求的格式。在这种情况下,一些最重要的可能问题是:

  1. 确保使用正确的“Content-Length”和“Content-Type”标头。 “内容类型”应为"multipart/form-data; boundary=YOUR_ACTUAL_BOUNDARY"
  2. 确保您没有忘记在结尾边界添加2个额外的' - '字符。
相关问题