In my application for Blackberry OS 6.0 I am posting multiple image files using HttpConnection, here is what I am trying,
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();
开发者_JAVA技巧 _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);
}
}
}
in my code I am using User-Agent as Profile/MIDP-2.0 Configuration/CLDC-1.0. Is this making any problem to post multiple files? Or is there any another way to post the data. The code doesn't through any exception but is enable to post Image files only. What I am missing in my code?
in my code I am using User-Agent as Profile/MIDP-2.0 Configuration/CLDC-1.0. Is this making any problem to post multiple files?
No. I would also recommend to not touch this header, because it is expected that the OS will set it in a most proper way.
UPDATE: Thanks to gnuf - I was incorrect saying "the OS will set it in a most proper way". It turns out there will be no such header at all, unless you set it. However I don't think this could be a reason, unless you instructed the server to reject requests if they don't have User-Agent header (which is unlikely).
Or is there any another way to post the data.
There may be a range of reasons. Basically what you need is to send a POST request of a 'multipart/form-data' type. So you need to correspond to the format of a such request. Some most important possible issues in this case are:
- Make sure you are using proper "Content-Length" and "Content-Type" headers. "Content-Type" should be
"multipart/form-data; boundary=YOUR_ACTUAL_BOUNDARY"
- Make sure you didn't forget to add 2 extra '-' chars to the ending boundary.
精彩评论