Spring stomp websocket "Incomplete frame, resetting input buffer..." - spring-websocket

I use the spring stomp websocket as the msg-push server endpoint. When I use the web browser to connect to it , both the browser and server works well. But recently, When I use Cocos creator's app simulator to connect to my server, it always fail to connect properly. There is no error message on the server and no prompt from the client. When debugging, I found that the connection request of the simulator could only be intercepted by the handshake interceptor, but not into the ClientInboundChannelInterceptor.
After some research, I found some error logs at TRACE level as following:
2019-03-01 11:34:09.433 INFO [msg-push,76064c86f7a1571f,76064c86f7a1571f,true] 5300 --- [nio-9006-exec-1] c.x.m.s.i.ClientHandshakeInterceptor : before handshake --> http://172.18.3.39:9005/stomp-ws
2019-03-01 11:34:09.456 INFO [msg-push,76064c86f7a1571f,76064c86f7a1571f,true] 5300 --- [nio-9006-exec-1] c.x.m.s.i.ClientHandshakeInterceptor : after handshake --> websocket
2019-03-01 11:34:09.487 TRACE [msg-push,,,] 5300 --- [nio-9006-exec-1] o.s.messaging.simp.stomp.StompDecoder : Incomplete frame, resetting input buffer...
2019-03-01 11:34:09.487 TRACE [msg-push,,,] 5300 --- [nio-9006-exec-1] o.s.w.s.m.StompSubProtocolHandler : Incomplete STOMP frame content received in session StandardWebSocketSession[id=0, uri=/stomp-ws], bufferSize=95, bufferSizeLimit=65536.
It seems taht there are some problem with the decoding method of the class org.spring framework.messaging.simp.stomp.StompDecoder. The code for the method is as follows:
/**
* Decode a single STOMP frame from the given {#code buffer} into a {#link Message}.
*/
#Nullable
private Message<byte[]> decodeMessage(ByteBuffer byteBuffer, #Nullable MultiValueMap<String, String> headers) {
Message<byte[]> decodedMessage = null;
skipLeadingEol(byteBuffer);
// Explicit mark/reset access via Buffer base type for compatibility
// with covariant return type on JDK 9's ByteBuffer...
Buffer buffer = byteBuffer;
buffer.mark();
String command = readCommand(byteBuffer);
if (command.length() > 0) {
StompHeaderAccessor headerAccessor = null;
byte[] payload = null;
if (byteBuffer.remaining() > 0) {
StompCommand stompCommand = StompCommand.valueOf(command);
headerAccessor = StompHeaderAccessor.create(stompCommand);
initHeaders(headerAccessor);
readHeaders(byteBuffer, headerAccessor);
payload = readPayload(byteBuffer, headerAccessor);
}
if (payload != null) {
if (payload.length > 0) {
StompCommand stompCommand = headerAccessor.getCommand();
if (stompCommand != null && !stompCommand.isBodyAllowed()) {
throw new StompConversionException(stompCommand +
" shouldn't have a payload: length=" + payload.length + ", headers=" + headers);
}
}
headerAccessor.updateSimpMessageHeadersFromStompHeaders();
headerAccessor.setLeaveMutable(true);
decodedMessage = MessageBuilder.createMessage(payload, headerAccessor.getMessageHeaders());
if (logger.isTraceEnabled()) {
logger.trace("Decoded " + headerAccessor.getDetailedLogMessage(payload));
}
}
else {
logger.trace("Incomplete frame, resetting input buffer...");
if (headers != null && headerAccessor != null) {
String name = NativeMessageHeaderAccessor.NATIVE_HEADERS;
#SuppressWarnings("unchecked")
MultiValueMap<String, String> map = (MultiValueMap<String, String>) headerAccessor.getHeader(name);
if (map != null) {
headers.putAll(map);
}
}
buffer.reset();
}
}
else {
StompHeaderAccessor headerAccessor = StompHeaderAccessor.createForHeartbeat();
initHeaders(headerAccessor);
headerAccessor.setLeaveMutable(true);
decodedMessage = MessageBuilder.createMessage(HEARTBEAT_PAYLOAD, headerAccessor.getMessageHeaders());
if (logger.isTraceEnabled()) {
logger.trace("Decoded " + headerAccessor.getDetailedLogMessage(null));
}
}
return decodedMessage;
}
we can see if the payload == null, then the error logs occurrs:
logger.trace("Incomplete frame, resetting input buffer...");
The above situation will cause the message frame not to be processed correctly that received by the method: org.springframework.web.socket.messaging.StompSubProtocolHandler.handleMessageFromClient(). As the code messages = decoder.decode(byteBuffer); will return a empty list, so I received the log Incomplete STOMP frame content ...
Part of the code is as follows:
List<Message<byte[]>> messages;
try {
ByteBuffer byteBuffer;
if (webSocketMessage instanceof TextMessage) {
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes());
}
else if (webSocketMessage instanceof BinaryMessage) {
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload();
}
else {
return;
}
BufferingStompDecoder decoder = this.decoders.get(session.getId());
if (decoder == null) {
throw new IllegalStateException("No decoder for session id '" + session.getId() + "'");
}
messages = decoder.decode(byteBuffer);
if (messages.isEmpty()) {
if (logger.isTraceEnabled()) {
logger.trace("Incomplete STOMP frame content received in session " +
session + ", bufferSize=" + decoder.getBufferSize() +
", bufferSizeLimit=" + decoder.getBufferSizeLimit() + ".");
}
return;
}
}
I don't know why the message frame will be decoded to the null, who can help me ?
Supplement:
Client connection code like this:
var client = Stomp.client("ws://172.18.3.39:9005/msgpush/stomp-ws/");
var headers = {
login: 'mylogin',
passcode: 'mypasscode',
};
client.connect(headers, connectCallback);
Development environment:
springboot 2.0.3.RELEASE

Related

Fetch parameters from redirected url in OAuth2 in java

I need to use OAuth 2.0 for accessing user's data.
I have used glassfish security oauth 2 library to implement OAuth 2 client.
I am not sure how can I get code and state values after user granted permission to access the data.
ClientIdentifier clientIdentifier = new ClientIdentifier(clientId, secret);
OAuth2CodeGrantFlow.Builder builder =
OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientIdentifier,
HOST_NAME + "/authorize",
HOST_NAME + "/token");
OAuth2CodeGrantFlow flow = builder
.scope("activity")
.redirectUri("http://example.com/#/")
.build();
String authorizationUri = flow.start();
System.out.println(authorizationUri);
String redirectedUrl = getFinalRedirectedUrl(authorizationUri);
System.out.print("Enter the authorization code: ");
String code = "";
String state = "";
try {
code = IN.readLine();
state = IN.readLine();
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
final TokenResult result = flow.finish(code, state);
System.out.println("Access Token: " + result.getAllProperties());
}
For Now, I am taking code and status manually from the redirected url. How Can I automate it.
I tried
public static String getFinalRedirectedUrl(String url) {
String finalRedirectedUrl = url;
try {
HttpURLConnection connection;
do {
connection = (HttpURLConnection) new URL(finalRedirectedUrl).openConnection();
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 300 && responseCode < 400) {
String redirectedUrl = connection.getHeaderField("Location");
if (null == redirectedUrl) {
break;
}
finalRedirectedUrl = redirectedUrl;
} else
break;
} while (connection.getResponseCode() != HttpURLConnection.HTTP_OK);
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(finalRedirectedUrl);
return finalRedirectedUrl;
}
but it returns the login page.

Mp4 cannot play on iOS by request spring mvc server resource, but working on Nginx access mp4 file directly

I have two scenario when play mp4 file on iOS devices.
MP4 access by Nginx is working:
Put mp4 file in /html and using following configure, then iOS devices and chrome browser can play mp4 files properly.
server {
listen 127.0.0.1:80 default_server;
location ~ ^/storage\/*.mp4 {
root html;
}
}
MP4 access by Tomcat Spring MVC is not working: When I request by Spring mvc restful API and return ResponseEntity contains Resource Object, in chrome browser will receive and play mp4 properly. But iOS devices not working
#GetMapping(value = "/storage/{filename:.+}")
#ResponseBody
public ResponseEntity<org.springframework.core.io.Resource> accessStorageFile(HttpServletResponse response, #PathVariable String filename) throws IOException {
org.springframework.core.io.Resource resource = storageUtil.loadAsResource(filename);
InputStream inputStream = resource.getInputStream();
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType(resource.getFile());
contentType = resource.getFilename().contains(".mp4") ? "video/mp4" : contentType;
response.setContentType(contentType);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.valueOf(contentType));
responseHeaders.setContentLength(resource.getFile().length());
return new ResponseEntity<>(inputStreamResource, responseHeaders, HttpStatus.OK);
}
import java.io.BufferedInputStream; import java.io.File; import
java.io.IOException; import java.io.InputStream; import
java.io.OutputStream; import java.nio.file.Files; import
java.nio.file.Path; import java.nio.file.Paths; import
java.nio.file.attribute.FileTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneOffset; import
java.util.ArrayList; import java.util.Arrays; import java.util.List;
import javax.servlet.ServletOutputStream; import
javax.servlet.http.HttpServletRequest; import
javax.servlet.http.HttpServletResponse; import
org.springframework.util.StringUtils;
/** * * #author David 1 */ public class MultipartFileSender {
private static final int DEFAULT_BUFFER_SIZE = 20480; // ..bytes = 20KB.
private static final long DEFAULT_EXPIRE_TIME = 604800000L; // ..ms = 1 week.
private static final String MULTIPART_BOUNDARY = "MULTIPART_BYTERANGES";
Path filepath;
HttpServletRequest request;
HttpServletResponse response;
public MultipartFileSender() {
}
public static MultipartFileSender fromPath(Path path) {
return new MultipartFileSender().setFilepath(path);
}
public static MultipartFileSender fromFile(File file) {
return new MultipartFileSender().setFilepath(file.toPath());
}
public static MultipartFileSender fromURIString(String uri) {
return new MultipartFileSender().setFilepath(Paths.get(uri));
}
//** internal setter **//
private MultipartFileSender setFilepath(Path filepath) {
this.filepath = filepath;
return this;
}
public MultipartFileSender with(HttpServletRequest httpRequest) {
request = httpRequest;
return this;
}
public MultipartFileSender with(HttpServletResponse httpResponse) {
response = httpResponse;
return this;
}
public void serveResource() throws Exception {
if (response == null || request == null) {
return;
}
if (!Files.exists(filepath)) {
System.out.println("File doesn't exist at URI : {" + filepath.toAbsolutePath().toString() + "}");
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
Long length = Files.size(filepath);
String fileName = filepath.getFileName().toString();
FileTime lastModifiedObj = Files.getLastModifiedTime(filepath);
if (StringUtils.isEmpty(fileName) || lastModifiedObj == null) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
long lastModified = LocalDateTime.ofInstant(lastModifiedObj.toInstant(),
ZoneId.of(ZoneOffset.systemDefault().getId())).toEpochSecond(ZoneOffset.UTC);
String contentType = "video/mp4";
// Validate request headers for caching ---------------------------------------------------
// If-None-Match header should contain "*" or ETag. If so, then return 304.
String ifNoneMatch = request.getHeader("If-None-Match");
if (ifNoneMatch != null && HttpUtils.matches(ifNoneMatch, fileName)) {
response.setHeader("ETag", fileName); // Required in 304.
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// If-Modified-Since header should be greater than LastModified. If so, then return 304.
// This header is ignored if any If-None-Match header is specified.
long ifModifiedSince = request.getDateHeader("If-Modified-Since");
if (ifNoneMatch == null && ifModifiedSince != -1 && ifModifiedSince + 1000 > lastModified) {
response.setHeader("ETag", fileName); // Required in 304.
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
// Validate request headers for resume ----------------------------------------------------
// If-Match header should contain "*" or ETag. If not, then return 412.
String ifMatch = request.getHeader("If-Match");
if (ifMatch != null && !HttpUtils.matches(ifMatch, fileName)) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
// If-Unmodified-Since header should be greater than LastModified. If not, then return 412.
long ifUnmodifiedSince = request.getDateHeader("If-Unmodified-Since");
if (ifUnmodifiedSince != -1 && ifUnmodifiedSince + 1000 <= lastModified) {
response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
return;
}
// Validate and process range -------------------------------------------------------------
// Prepare some variables. The full Range represents the complete file.
Range full = new Range(0, length - 1, length);
List<Range> ranges = new ArrayList<>();
// Validate and process Range and If-Range headers.
String range = request.getHeader("Range");
if (range != null) {
// Range header should match format "bytes=n-n,n-n,n-n...". If not, then return 416.
if (!range.matches("^bytes=\\d*-\\d*(,\\d*-\\d*)*$")) {
response.setHeader("Content-Range", "bytes */" + length); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
String ifRange = request.getHeader("If-Range");
if (ifRange != null && !ifRange.equals(fileName)) {
try {
long ifRangeTime = request.getDateHeader("If-Range"); // Throws IAE if invalid.
if (ifRangeTime != -1) {
ranges.add(full);
}
} catch (IllegalArgumentException ignore) {
ranges.add(full);
}
}
// If any valid If-Range header, then process each part of byte range.
if (ranges.isEmpty()) {
for (String part : range.substring(6).split(",")) {
// Assuming a file with length of 100, the following examples returns bytes at:
// 50-80 (50 to 80), 40- (40 to length=100), -20 (length-20=80 to length=100).
long start = Range.sublong(part, 0, part.indexOf("-"));
long end = Range.sublong(part, part.indexOf("-") + 1, part.length());
if (start == -1) {
start = length - end;
end = length - 1;
} else if (end == -1 || end > length - 1) {
end = length - 1;
}
// Check if Range is syntactically valid. If not, then return 416.
if (start > end) {
response.setHeader("Content-Range", "bytes */" + length); // Required in 416.
response.sendError(HttpServletResponse.SC_REQUESTED_RANGE_NOT_SATISFIABLE);
return;
}
// Add range.
ranges.add(new Range(start, end, length));
}
}
}
// Prepare and initialize response --------------------------------------------------------
// Get content type by file name and set content disposition.
String disposition = "inline";
// If content type is unknown, then set the default value.
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
// To add new content types, add new mime-mapping entry in web.xml.
if (contentType == null) {
contentType = "application/octet-stream";
} else if (!contentType.startsWith("image")) {
// Else, expect for images, determine content disposition. If content type is supported by
// the browser, then set to inline, else attachment which will pop a 'save as' dialogue.
String accept = request.getHeader("Accept");
disposition = accept != null && HttpUtils.accepts(accept, contentType) ? "inline" : "attachment";
}
System.out.println("Content-Type : {" + contentType + "}");
// Initialize response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Type", contentType);
response.setHeader("Content-Disposition", disposition + ";filename=\"" + fileName + "\"");
System.out.println("Content-Disposition : {" + disposition + "}");
response.setHeader("Accept-Ranges", "bytes");
response.setHeader("ETag", fileName);
response.setDateHeader("Last-Modified", lastModified);
response.setDateHeader("Expires", System.currentTimeMillis() + DEFAULT_EXPIRE_TIME);
// Send requested file (part(s)) to client ------------------------------------------------
// Prepare streams.
try (InputStream input = new BufferedInputStream(Files.newInputStream(filepath));
OutputStream output = response.getOutputStream()) {
if (ranges.isEmpty() || ranges.get(0) == full) {
// Return full file.
System.out.println("Return full file");
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + full.start + "-" + full.end + "/" + full.total);
response.setHeader("Content-Length", String.valueOf(full.length));
Range.copy(input, output, length, full.start, full.length);
} else if (ranges.size() == 1) {
// Return single part of file.
Range r = ranges.get(0);
System.out.println("Return 1 part of file : from ({" + r.start + "}) to ({" + r.end + "})");
response.setContentType(contentType);
response.setHeader("Content-Range", "bytes " + r.start + "-" + r.end + "/" + r.total);
response.setHeader("Content-Length", String.valueOf(r.length));
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Copy single part range.
Range.copy(input, output, length, r.start, r.length);
} else {
// Return multiple parts of file.
response.setContentType("multipart/byteranges; boundary=" + MULTIPART_BOUNDARY);
response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT); // 206.
// Cast back to ServletOutputStream to get the easy println methods.
ServletOutputStream sos = (ServletOutputStream) output;
// Copy multi part range.
for (Range r : ranges) {
System.out.println("Return multi part of file : from ({" + r.start + "}) to ({" + r.end + "})");
// Add multipart boundary and header fields for every range.
sos.println();
sos.println("--" + MULTIPART_BOUNDARY);
sos.println("Content-Type: " + contentType);
sos.println("Content-Range: bytes " + r.start + "-" + r.end + "/" + r.total);
// Copy single part range of multi part range.
Range.copy(input, output, length, r.start, r.length);
}
// End with multipart boundary.
sos.println();
sos.println("--" + MULTIPART_BOUNDARY + "--");
}
}
}
private static class Range {
long start;
long end;
long length;
long total;
/**
* Construct a byte range.
*
* #param start Start of the byte range.
* #param end End of the byte range.
* #param total Total length of the byte source.
*/
public Range(long start, long end, long total) {
this.start = start;
this.end = end;
this.length = end - start + 1;
this.total = total;
}
public static long sublong(String value, int beginIndex, int endIndex) {
String substring = value.substring(beginIndex, endIndex);
return (substring.length() > 0) ? Long.parseLong(substring) : -1;
}
private static void copy(InputStream input, OutputStream output, long inputSize, long start, long length) throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int read;
if (inputSize == length) {
// Write full range.
while ((read = input.read(buffer)) > 0) {
output.write(buffer, 0, read);
output.flush();
}
} else {
input.skip(start);
long toRead = length;
while ((read = input.read(buffer)) > 0) {
if ((toRead -= read) > 0) {
output.write(buffer, 0, read);
output.flush();
} else {
output.write(buffer, 0, (int) toRead + read);
output.flush();
break;
}
}
}
}
}
private static class HttpUtils {
/**
* Returns true if the given accept header accepts the given value.
*
* #param acceptHeader The accept header.
* #param toAccept The value to be accepted.
* #return True if the given accept header accepts the given value.
*/
public static boolean accepts(String acceptHeader, String toAccept) {
String[] acceptValues = acceptHeader.split("\\s*(,|;)\\s*");
Arrays.sort(acceptValues);
return Arrays.binarySearch(acceptValues, toAccept) > -1
|| Arrays.binarySearch(acceptValues, toAccept.replaceAll("/.*$", "/*")) > -1
|| Arrays.binarySearch(acceptValues, "*/*") > -1;
}
/**
* Returns true if the given match header matches the given value.
*
* #param matchHeader The match header.
* #param toMatch The value to be matched.
* #return True if the given match header matches the given value.
*/
public static boolean matches(String matchHeader, String toMatch) {
String[] matchValues = matchHeader.split("\\s*,\\s*");
Arrays.sort(matchValues);
return Arrays.binarySearch(matchValues, toMatch) > -1
|| Arrays.binarySearch(matchValues, "*") > -1;
}
} }
use is in your controller and make sure you are not use #RestController
you should use #Controller, and following class in void method
#RequestMapping(method = RequestMethod.GET, value = "/getFile")
public void getFile(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws Exception {
MultipartFileSender.fromPath(Paths.get("D:\8561523973120206.mp4"))
.with(httpRequest)
.with(httpResponse)
.serveResource();
}

Display HTTP Request Information - BlackBerry

How can I get the all HTTP request headers, method, the suffix of the connection, and all parameters that I added to the request?
Try something like this (I ran this code on a background thread, which I why I use UiApplication.invokeLater() to display results):
try {
ConnectionFactory factory = new ConnectionFactory(); // for OS 5.0+
factory.setPreferredTransportTypes(new int[] {
TransportInfo.TRANSPORT_TCP_WIFI,
TransportInfo.TRANSPORT_TCP_CELLULAR
});
// For OS < 5.0
//HttpConnection conn = (HttpConnection) Connector.open("http://www.google.com;interface=wifi");
HttpConnection conn = (HttpConnection) factory.getConnection("http://www.google.com").getConnection();
conn.setRequestProperty("sessionId", "ABCDEF0123456789");
final StringBuffer results = new StringBuffer();
String key = "";
int index = 0;
// loop over all the header fields, and record their values
while (key != null) {
key = conn.getHeaderFieldKey(index);
if (key != null) {
String value = conn.getHeaderField(key);
results.append(key + " = " + value + "\n\n");
}
index++;
}
results.append("method = " + conn.getRequestMethod() + "\n\n");
// we (should) know which request properties we've set, so we ask
// for them by name here
String sessionId = conn.getRequestProperty("sessionId");
results.append("sessionId = " + sessionId + "\n\n");
String url = conn.getURL();
results.append("URL = " + url);
// show the result on screen (UI thread)
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
textField.setText(results.toString());
}
});
} catch (IOException e) {
e.printStackTrace();
}

wifi doesn't work in own apps, but works in blackberry's browser

we have just started to write Blackberry apps and got strange situation. Our apps work with mobile internet (GPRS, 3G, EDGE) but are not working using wifi connection.
I have tried to change all the settings. But still usually it is just "Tunnel failure" or "connection timed out" errors. The same is with HTTPDemo example.
Could someone help and explain what it is with Blackberry and WiFi?
StreamConnection s = null;
s = (StreamConnection)Connector.open(getUrl() +";interface=wifi");
HttpConnection httpConn = (HttpConnection)s;
int status = httpConn.getResponseCode();
if (status == HttpConnection.HTTP_OK)
{
// Is this html?
String contentType = httpConn.getHeaderField(HEADER_CONTENTTYPE);
boolean htmlContent = (contentType != null && contentType.startsWith(CONTENTTYPE_TEXTHTML));
InputStream input = s.openInputStream();
byte[] data = new byte[256];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while ( -1 != (len = input.read(data)) )
{
// Exit condition for the thread. An IOException is
// thrown because of the call to httpConn.close(),
// causing the thread to terminate.
if ( _stop )
{
httpConn.close();
s.close();
input.close();
}
raw.append(new String(data, 0, len));
size += len;
}
raw.insert(0, "bytes received]\n");
raw.insert(0, size);
raw.insert(0, '[');
content = raw.toString();
if ( htmlContent )
{
content = prepareData(raw.toString());
}
input.close();
}
else
{
content = "response code = " + status;
}
s.close();
}
catch (IOCancelledException e)
{
System.out.println(e.toString());
return;
}
catch (IOException e)
{
errorDialog(e.toString());
return;
}
Connecting the following way works for me
HttpConnection connection = null;
if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
connection = (HttpConnection) Connector.open(url+ ";interface=wifi",
Connector.READ_WRITE,true);
} else {
connection = (HttpConnection) Connector.open(url+";deviceside=true", Connector.READ_WRITE,true);
}
Please refer the following resource for in-depth understanding and various methods.
Sample HTTP Connection code and BIS-B Access By peter_strange

What is correct way to implement the HTTPConnection in Blackberry

I tired to connect my app with the remote server and pass few credentials to it, but i am always getting a same response from the server. I tried changing all the parameter value and other request header values that i am passing, but still i can't reach the exact solution. I need to you, whether am i using the correct way to ping with the server and to pass the values.
Below is the code that i am using , Please let me know if i have gone wrong somewhere.
// HttpServiceConnection.java
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.HttpsConnection;
import net.rim.device.api.system.DeviceInfo;
import com.beacon.bb.app.util.WSMConfig;
/**
* #author N********
*
*/
public class HttpServiceCommunication {
public HttpServiceCommunication() {
System.out.println("Http Service Communication Called");
}
public String sendHttpPost(String uri, String email, String uid,
String pass) throws Exception { // Hashtable header
String response = null;
// create the connection...
System.out.println("Url    " + uri);
HttpConnection _connection = null;
String params = null;
if (DeviceInfo.isSimulator()) {
params = ";deviceside=false";
} else {
params = ";deviceside=true;interface=wifi";
}
String URL = uri + params;
System.out.println("Connecting to Http Connection ");
try {
_connection = (HttpConnection) Connector.open(URL);
} catch(Exception e){
e.printStackTrace();
}
if (_connection != null) {
_connection.setRequestMethod(HttpConnection.POST);
System.out.println("After Request Method ");
_connection.setRequestProperty("User-Agent",
"Profile/MIDP-2.0 Configuration/CLDC-1.1");
_connection.setRequestProperty("Content-Language", "en-US");
_connection.setRequestProperty("Content-type", "application/json");
// setting header if any
// if (header != null) {
// for (Enumeration en = header.keys(); en.hasMoreElements();) {
// String key = (String) en.nextElement();
// String value = (String) header.get(key);
// _connection.setRequestProperty(key, value);
_connection.setRequestProperty("email", email);
//_connection.setRequestProperty("method","login");
_connection.setRequestProperty("uid", uid);
_connection.setRequestProperty("password", pass);
//_connection.setRequestProperty("uid", uid);
// }
// }
System.out.println("Open Output Stream  ");
// System.out.println("Data is     "+data);
OutputStream _outputStream = _connection.openOutputStream();
//System.out.println("Writing data  ");
//_outputStream.write(data);
// _outputStream.flush(); // Optional, getResponseCode will flush
// Getting the response code will open the connection, send the
// request, and read the HTTP response headers.
// The headers are stored until requested.
try {
System.out.println("Response Code :" + _connection.getResponseCode());
int rc = _connection.getResponseCode();
System.out.println("Response Code :" + rc);
System.out.println("Response Code   :" + rc + " if HTTP OK    :"
+ (rc == HttpConnection.HTTP_OK));
if (rc == HttpConnection.HTTP_FORBIDDEN) {
System.out.println("FORBIDDEN");
response = WSMConfig.NOT_AUTH;
} else if (rc != HttpConnection.HTTP_OK) {
response = WSMConfig.NOT_OK;
} else if (rc == HttpConnection.HTTP_OK) {
InputStream _inputStream = _connection.openInputStream();
final int MAX_LENGTH = 128;
byte[] buf = new byte[MAX_LENGTH];
int total = 0;
while (total < MAX_LENGTH) {
int count = _inputStream.read(buf, total, MAX_LENGTH
- total);
if (count < 0) {
break;
}
total += count;
}
response = new String(buf, 0, total);
//ByteBuffer bb = new ByteBuffer(_inputStream);
//response = bb.getString();
System.out.println("Response from Server   :" + response);
// close everything out
{
if (_inputStream != null)
try {
_inputStream.close();
} catch (Exception e) {
}
if (_outputStream != null)
try {
_outputStream.close();
} catch (Exception e) {
}
if (_connection != null)
try {
_connection.close();
} catch (Exception e) {
}
}
}
else {
response = WSMConfig.SERVER_ERROR;
}
}catch(Exception e){
e.printStackTrace();
}
}
//System.out.println("Response :" + response);
return response;
}
}
I am getting a response like {"code":0,"err":"Missing 'method'."}
Any Help is Appreciable....
Thanks
Try this out when you're wanting to pass data to the server:
//encode your data to send
URLEncodedPostData encoder = new URLEncodedPostData(null, false);
encoder.encode("email", email);
encoder.encode("method", "login");
encoder.encode("uid", uid);
encoder.encode("password", pass);
//Now you open up an output stream to write to the connection
OutputStream os = _connection.openOutputStream();
os.write(encoder.getBytes();
os.flush();
And then continue with the rest of your logic

Resources