I am struggling for getting message on devices since last some days. At last I think to take a help from experts.
Below are the steps I have followed.
I have created Push initiator in core java.
note: I am getting response code : 200 OK - I assume that this message reaches to PPG for further processing - But not confirmed.
I have used sample Push Enabled Application for Blackberry Device -
I could able to register and waiting for messages on perticular port. but I am not receiving any messages :(
Now I am helpless, My question is
1. Does I am in right track
2. Is there any way to find out how to debug and where my messages are hanging.
I am waiting for some positive solution. Thanks in advance..
Here is my actual code for Push initiator for J2SE
StringBuffer dataToSend = new StringBuffer();
dataToSend.append("--" + BOUNDARY);
dataToSend.append(" ");
dataToSend.append("Content-Type: application/xml; charset=UTF-8");
dataToSend.append(" ");
dataToSend.append("<?xml version=\"1.0\"?>");
dataToSend
.append("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\" [<?wap-pap-ver supported-versions=\"2.0\"?>]>");
dataToSend.append("<pap>");
String myPushId = ""+ new Random().nextInt();
dataToSend.append("<push-message push-id=\"" + myPushId
+ "\" source-reference=\"" + applicationID + "\">");
dataToSend.append("<address address-value=\"" + pin + "\"/>");
dataToSend
.append("<quality-of-service delivery-method=\"confirmed\"/>");
dataToSend.append("</push-message>");
dataToSend.append("</pap> ");
dataToSend.append("--" + BOUNDARY);
dataToSend.append(" Content-Encoding: binary ");
dataToSend.append(" Content-Type: text/plain ");
dataToSend.append(" ");
dataToSend.append("X-Wap-Application-Id: " + applicationID);
dataToSend.append(" X-Rim-Push-Type: browser-channel X-Rim-Push-Title: Test X-Rim-Push-Unread-Icon: http://rim.com/icon_new.png ");
dataToSend.append("X-Rim-Push-Read-Icon: http://rim.com/icon_viewed.png ");
dataToSend.append("X-Rim-Delete-URL: http://rim.com/ReceiveDelete ");
dataToSend.append("X-Rim-Transcode-Content: */* ");
dataToSend.append("Cache-Control: max-age=3600 ");
dataToSend.append(msg);
dataToSend.append("--" + BOUNDARY + "--");
dataToSend.append(" ");
printer(dataToSend.toString());
// printer("-------------------------------------------------------------------");
String authInfo = applicationID + ":" + userPW;
String encoding = new sun.misc.BASE64Encoder().encode(authInfo
.getBytes());
// authInfo = Base64.encode(authInfo.getBytes());
authInfo = "Basic " + encoding;
printer(authInfo);
URL url;
HttpURLConnection connection = null;
try {
// Create connection
url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"multipart/related; boundary=" + BOUNDARY
+ "; type=application/xml");
connection.setRequestProperty("User-Agent",
"BlackBerry Push Service SDK/1.0");
// / connection.setRequestProperty("Authorization",
// authInfo.getBytes().toString());
connection.setRequestProperty("Authorization", authInfo);
connection.setRequestProperty("Accept",
"text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("Content-Length",
"" + dataToSend.length());
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(dataToSend.toString());
wr.flush();
wr.close();
printer("" + connection.getResponseCode());
String response = connection.getResponseMessage();
printer(response);
// return response;
Related
I tried to use the following method which is used in other areas of the app but I still get 401:
The original question is posted here: Java 6 EE Client Credentials Bearer
String urlParameters = "client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=client_credentials";
byte[] postData = urlParameters.getBytes("UTF-8");
int postDataLength = postData.length;
String TokenURL = "URL for getting the token here";
URL url = new URL(TokenURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
wr.flush();
System.out.println("->->-> We will send: " + urlParameters);
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED && conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.println("->->-> Returned Http Code: " + conn.getResponseCode());
throw new RuntimeException("Failed : HTTP error code for Getting Token XFORMURLENCODED : " +
conn.getResponseCode());
}
I should send text/image files to one website using POST method.
But, I couldn't get 200 code after my MainActivity.
my code block
String crlf = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
System.out.println("[Request] routine is started.");
URL url = new URL(_url);
urlConn = (HttpURLConnection) url.openConnection();
// [2-1]. urlConn setting
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestMethod("POST"); // URL method : POST.
urlConn.setRequestProperty("Connection", "keep-alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
urlConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
// [2-2]. parameter send and read data
OutputStream os = urlConn.getOutputStream();
DataOutputStream request = new DataOutputStream(os);
// Write key and values
// 1. Access Key
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"accessKey\"" + crlf);
request.writeBytes("Content-Type: application/json" + crlf);
request.writeBytes(crlf);
request.writeBytes(accessKey + crlf);
// 2. Image data
request.writeBytes(twoHyphens + boundary + crlf);
request.writeBytes("Content-Disposition: form-data; name=\"files\"" + crlf);
request.writeBytes("Content-Type: image/jpeg" + crlf);
request.writeBytes(crlf);
request.write(imageBytes);
request.writeBytes(crlf);
request.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
request.flush();
request.close();
int response_code = urlConn.getResponseCode();
System.out.println("Connection response code : [" + response_code + "]");
I think my DataOutputStream code is not bad, but I get 400 error:
I/System.out: [Request] routine is started.
D/EGL_emulation: app_time_stats: avg=45.59ms min=8.56ms max=670.08ms count=23
I/System.out: Connection response code : [400]
I/System.out: [doInBackground] output : Response Code : 400
I/System.out: [onPostExecute] routine is started.
I/System.out: [onPostExecute] output : Response Code : 400
What's wrong?
I need to send a request with GET method to server. But, after
'DataOutputStream output = new DataOutputStream(connection.getOutputStream());' this line, when i use 'connection.getRequestMethod()', I am getting method as POST. dont know why.
code is :
HttpURLConnection connection;
URL url1 = new URL(url);
LOGGER.info(participant.getTxnLogKey() + " URL...................: " + url1);
connection = (HttpURLConnection) url1.openConnection();
connection.setRequestMethod("GET");
LOGGER.info(participant.getTxnLogKey() + "connection.getRequestMethod().................: " + connection.getRequestMethod());
connection.setConnectTimeout(timeoutMs);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Host", StringUtils.substring(url, url.indexOf("//") + 2, url.indexOf("biz") + 3));
connection.setRequestProperty("User-Agent", "XXXXXXXXX");
connection.setRequestProperty("Connection", "keep-alive");
connection.setRequestProperty("cache-control", "no-cache");
connection.setRequestProperty("Content-length", "" + (request.length()));
connection.setRequestProperty("Content-Type", "text/xml");
LOGGER.info(participant.getTxnLogKey() + "connection.getRequestMethod().................: " + connection.getRequestMethod());
/* here, the connection.getRequestMethod() returns GET*/
DataOutputStream output = new DataOutputStream(connection.getOutputStream());
LOGGER.info(participant.getTxnLogKey() + "connection.getRequestMethod().................: " + connection.getRequestMethod());
/* here, the connection.getRequestMethod() returns POST. I need it as GET.*/
output.writeBytes(request);
output.flush();
When I remove DataoutputStream, I am getting PREMATURE END OF FILE.
I'm trying to send a Soap request to a server.
This is my java code:
try {
String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.telenet.be/TelemeterService/\">\n" +
" <SOAP-ENV:Body>\n" +
" <ns1:RetrieveUsageRequest>\n" +
" <UserId>test</UserId>\n" +
" <Password>test</Password>\n" +
" </ns1:RetrieveUsageRequest>\n" +
" </SOAP-ENV:Body>\n" +
"</SOAP-ENV:Envelope>";
URL myurl = new URL("https://t4t.services.telenet.be/TelemeterService?wsdl");
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-length", String.valueOf(xmldata.length()));
con.setRequestProperty("Content-Type", "text/xml; charset=\\\"utf-8\\\"\\r\\n");
con.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(xmldata);
output.close();
DataInputStream input = new DataInputStream(con.getInputStream());
for (int c = input.read(); c != -1; c = input.read())
System.out.print((char) c);
input.close();
System.out.println("Resp Code:" + con.getResponseCode());
System.out.println("Resp Message:" + con.getResponseMessage());
}
catch (IOException se)
{
se.printStackTrace();
}
I get the exception:
java.io.IOException: Server returned HTTP response code: 500 for URL: https://t4t.services.telenet.be/TelemeterService?wsdl
I used this tool to be sure of my request: http://wsdlbrowser.com/soapclient?wsdl_url=https%3A%2F%2Ft4t.services.telenet.be%2FTelemeterService%3Fwsdl&function_name=retrieveUsage
I don't see whats wrong with my request.
When I get an internal server error, the first thing I do (and maybe you did) is to check the application server log. For some unexpected reason(s) the server cannot be reached.
I don't see any problem to get the wsdl first to check if you can use the web service. It is an approach I do all the time.
The ?wsdl at the end of the url are for getting the wsdl, for example in a browser. Try removing it , making the endpoint URL https://t4t.services.telenet.be/TelemeterService.
I’m developing a small REST client for Java, and my using GitHub’s API while testing.
I can’t seem to figure out what i’v done wrong, here is the code that makes the https request.
Link to the repo: https://github.com/sigurdsvela/JREST
HttpURLConnection connection = (HttpURLConnection)ourl.openConnection();
if (protocol == Protocol.HTTPS) { //protocol is Protocol.HTTPS
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
((HttpsURLConnection) connection).setSSLSocketFactory(sslsocketfactory);
}
System.out.println("Sending " + method + " request to " + ourl + "?" + urlParameters);
connection.setRequestMethod(method.getName());
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.toString().getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream( connection.getOutputStream() );
wr.writeBytes(urlParameters.toString());
wr.flush();
wr.close();
System.out.println("Server responded with " + connection.getResponseCode() + " " + connection.getResponseMessage());
//Get Response
InputStream is = connection.getInputStream();
When I run this code i get a FileNotFound from the last line, and the System.out.println("Server responded whi.... print that the server responded with 404 Not Found.
The System.out.println("Sending " + method.... print out the url its requesting. I’ve copy pasted it into my browser, and then it works fine. Am i making some false assumption? Is there something i have forgotten.
I'm sending a get.
I've also tried to remove the if (protocol == Protocol.HTTPS) condition and have it always be a HttpsURLConnection, instead of casting it. That didn't work either, and I was still getting a 404 with the same message.
The URL I’m testing against, if relevant is:
api.github.com/repos/sigurdsvela/loveletters