HTTP Post with Matlab

2019-08-08 01:10发布

问题:

I followed the example given in this post: How to send data from Matlab to Rails, but am receiving an error message that I cannot find any information on. My script looks like this:

javaaddpath('./httpcomponents/httpclient-4.2.2.jar')
javaaddpath('./httpcomponents/httpcore-4.2.2.jar')

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

httpclient = DefaultHttpClient();

httppost = HttpPost('http://localhost:3000/signin');
httppost.addHeader('Content-Type','application/json');
httppost.addHeader('Accept','application/json');

tokenRequest = StringEntity('{"session", "{email_address:""email@aol.com,""password:""password""}"}');
httppost.setEntity(tokenRequest);

response = httpclient.execute(httppost);

On the last line, I get the error:

Java exception occurred: java.lang.VerifyError: Cannot inherit from final class

From searches online, I gather that this is a software version issue. I tried using the 4.2 builds of those files (the same ones used in the other post), but I received the same error. Does anyone have an idea what could be wrong? Or know a way to do what I am trying to do without using these external libraries?

EDIT:

originally I tried using this code:

tokenRequest = {'session', '{''email_address'':''email@aol.com'',''password'':''password''}'};
token = urlread('http://localhost:3000/signin','POST',tokenRequest);

but I received the NoMethodError that led me to that other post:

NoMethodError (undefined method `each' for "{'email_address':'email@aol.com','password':'password'}":String):
     app/models/session.rb:14:in `initialize'

I think the reason it throws this error is because the server thinks it is receiving a String object, which doesn't have an each method. I assume I would fix this by using the 'Content-Type' argument to specify that its json. Is there a way to do this using urlread?

EDIT: full stack trace for java libs issue

Java exception occurred:
java.lang.VerifyError: Cannot inherit from final class

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.access$000(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:157)

at  org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448)

at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309)

at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)

at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286)

at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851)

at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)

at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

回答1:

Looks like the data being sent is not properly quoted. End result should be

{'email_address':'email@aol.com','password':'password'}

Try changing the code to

tokenRequest = StringEntity('{"session", {"email_address":"email@aol.com","password":"password"}}');


回答2:

urlread2 turned out to be a much better solution. This code does the job:

tokenRequest = '{ "session" : { "email_address": "email@aol.com", "password": "password" } }';
header = http_createHeader('Content-Type','application/json');
token = urlread2('http://localhost:3000/signin.json','POST',tokenRequest,header);


标签: java http matlab