So, I get qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed when I try to send XMLHttpRequest.
I use Qt 5.12.2, imsvc2017_64. How can I solve this problem?
function requestCall(type){
var http = new XMLHttpRequest()
var url = "https://******";
var params = ""
params = "operator_id=1&call_id="+call_id;
console.log("Request (type): "+(type))
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form- urlencoded");
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() { }
http.send(params);
}
Adding OpenSSL Support for Android
https://doc.qt.io/qt-5/android-openssl-support.html
Related
I know there are many different situations that resemble mine across stackoverflow, but I just couldn't make the connection.
What I am trying to do, is to send a simple push notification to the GCM. I found two links for which I try to POST too. Note both these links work in this PHP script i found.
https://gcm-http.googleapis.com/gcm/send
https://android.googleapis.com/gcm/send
I tried to send push notifications from JS to the GCM, but many people have stated that this can not because of security issues. As of now, when I execute my code in Angular JS, I am getting a 405 error from https://gcm-http.googleapis.com/gcm/send. Status 405 means method not accepted (link for reference http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
Here is the code for JS. I have two method that I tried.
Method 1:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//ite
}
};
var jsonCall = {
registration_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-AEQtUUWnCVH566xcwib4HinI16W3_g"
};
xmlhttp.open("POST", "https://gcm-http.googleapis.com/gcm/send", true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("Authorization", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
xmlhttp.send(jsonCall);
Method 2
var jsonCall = {
registration_id: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx-AEQtUUWnCVH566xcwib4HinI16W3_g"
};
$http({
method:'POST',
url: 'https://gcm-http.googleapis.com/gcm/send',
data: jsonCall,
headers: {
'Authorization': 'A1nxxxxxxxxxxxxxxxxxx',
'Content-type': 'application/json' }
})
This is what I have tried in Java. Note that my project was not created as an Android project, but just as a normal Java project. I get a 411 error here, so I think the string I use as JSON is incorrect. Note that I get a 200 if I use GET.
Method 3:
HttpURLConnection connection = null;
try {
//Create connection
String json ="{\"registration_ids\":[\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxx\"]}";
URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Content-Length", "0");
connection.setRequestProperty("Authorization", "key="+"xxxxxxxxxxxxxxxxxxxxxxxxxx");
System.out.println(connection.getResponseCode());
InputStream stream = (InputStream) connection.getInputStream();
InputStreamReader isReader = new InputStreamReader(stream);
//put output stream into a string
BufferedReader br = new BufferedReader(isReader);
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
OutputStream os = connection.getOutputStream();
os.write(json.getBytes("UTF-8"));
os.flush();
os.close();
} catch(Exception e){
System.out.println(e);
}
If someone can take a look at this, and set me in the correct direction, I would really appreciate it.
UPDATE:
I have gotten rid of that 411 error. I think it was because I never connected in the first place. Now I am getting the correct 200 code, but the push notification does not send. Is my JSON the correct format?
HttpURLConnection connection = null;
try {
//Create connection
String json ="{\"registration_ids\":[\"APA91bGxHWapgmxgyvPceu85ArDMLaFekbTt5RGzy3gv1xtSO09tJbvnaeVLefBqNl_iBrctoZQ2AltSMfrXykq8-AEQtUUWnCVH566xcwib4HinI16W3_g\"]}";
URL url = new URL("https://gcm-http.googleapis.com/gcm/send");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "key=xxxxxxxxxxxxxxxxxxxxxxx");
connection.connect();
OutputStream os = connection.getOutputStream();
os.write(json.getBytes("UTF-8"));
System.out.println(connection.getResponseCode());
InputStream stream = (InputStream) connection.getInputStream();
InputStreamReader isReader = new InputStreamReader(stream);
//put output stream into a string
BufferedReader br = new BufferedReader(isReader);
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
os.flush();
os.close();
} catch(Exception e){
System.out.println(e);
}
This has been solved using the Java method. JS keeps on returning those status codes of 400, 401, 411 etc. It turns out the reason Java returned a 200 but my phone did not receive anything was because my JSON was incorrect. Here is the correct JSON value:
String postData = "{ \"registration_ids\": [ \"" + CLIENT_REG_ID + "\" ], " +
"\"delay_while_idle\": true, " +
"\"data\": {\"tickerText\":\"My Ticket\", " +
"\"contentTitle\":\"My Title\", " +
"\"message\": \"Test GCM message from GCMServer-Android\"}}";
This was obtained from another question I posted, where a fellow SO member provided this solution.
Hi people i'm trying to do a head request to get the content-length of the file (it's big >= 200MB) and simple just throw me an exception saying "Exception of type 'System.OutOfMemoryException' was thrown." since it's a HEAD method this shouldn't be a problem right?
Here is my code:
using (HttpClient client = new HttpClient())
{
using (var request = new HttpRequestMessage()
{
RequestUri = new Uri(streamLink.StreamLink),
Method = HttpMethod.Head
})
{
using (var response = await client.SendAsync(request))
{
var restatus = response.StatusCode == HttpStatusCode.OK;
if (restatus)
{
var filesize = ConvertBytesToMegabytes(response.Content.Headers.ContentLength);
CalculateStreamQuality(filesize, streamLink, runtime);
}
}
}
}
You need to call the SendAsync with the completion option parameter set to: HttpCompletionOption.ResponseHeadersRead - this will enable the SendAsync method to complete without trying to allocate a buffer to hold the (non-existent data payload).
Update your code to use the following:
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
{
...
}
I am new to jquery and don't know to fetch json data from another domain(Cross domain).
function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined"){
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
var request = createCORSRequest("get", "http://www.stackoverflow.com/");
if (request){
request.onload = function() {
// ...
};
request.onreadystatechange = handler;
request.send();
}
I found this program from here Ways to circumvent the same-origin policy
This is saying by using above code we can access cross domain json data.
I copied the code. This is saying handler is undefined
I don't know how to define handler .
I also don't know what to write inside request.onload
where I will get the json result
Please help
Thanks in advance
The handler is a function
it should be something like
function handler(){
var response = xhr.responseText;
// do more with your response.
}
Also you xhr should be defined outside of the function createCORSrequest.
See docs on XDR
I know you said you are new to jquery but you should also look into $.getJSON. Its much easier.
I am trying to hit .svc service from my JME application using POST method. but getting 'bad request'. Following is my code.
HttpConnection hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
hc.setRequestMethod(HttpConnection.POST);
hc.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0" );
hc.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
hc.setRequestProperty("Content-Length", ""+(postMsg.getBytes().length));
out = hc.openOutputStream();
out.write(postMsg.getBytes());
System.out.println("hc.getResponseCode() = "+hc.getResponseCode()+ " hc.getResponseMessage() = "+hc.getResponseMessage());
Please tell me what is wrong with the code.
Instead of http, I used Ksoap2-j2me-core jar with following code that i found -
SoapObject request = new SoapObject("namespace", "login");
request.addProperty("username", "pranav");
request.addProperty("password", "gangan");
//create the SOAP envelope
final SoapSerializationEnvelope env = new SoapSerializationEnvelope(SoapEnvelope.VER11);
env.setOutputSoapObject(request);
//create the transport and then call
final HttpTransport httpTransport = new HttpTransport("http://URL");
httpTransport.call("\"login\"", env);
SoapObject body = (SoapObject) env.bodyIn;
//body.getProperty(0) will return the content of the first tag inside body
Object response = body.getProperty(0);
System.out.println(response.toString);
I have a certificate file which I want to import into flex application to establish a secure socket connection with a sever. But I am a getting an exception which says ArgumentError: Error #2004: One of the parameters is invalid.
var urlLoader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("xyz.crt");
urlLoader.addEventListener(Event.COMPLETE, doEvent);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(request);
private function doEvent(evt:Event):void {
var ldr:URLLoader = evt.target as URLLoader;
secureSocket.addEventListener( Event.CONNECT, onConnect )
secureSocket.addEventListener( IOErrorEvent.IO_ERROR, onError );
try {
secureSocket.addBinaryChainBuildingCertificate(ldr.data,true );
secureSocket.connect( "192.168.2.100", 443 );
} catch ( error:Error ) {
Alert.show( error.toString() );
}
For me, the reason is because the certificat is encoded in PEM format.
After convert it into DER format, it works fine.
A ByteArray object containing a DER-encoded X.509 digital certificate.