curl - Unable to post the data to server, getting error java.io.IOException: Server returned HTTP response code: 415 -
iam unable post data server, getting error . working fine in curl script.
error reading url java.io.ioexception: server returned http response code: 415 url: https://8.7.177.4/api/domains/amj.nms.mixnetworks.net/subscribers/9001?do_not_disturb=no @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source) @ sun.net.www.protocol.https.httpsurlconnectionimpl.getinputstream(unknown source) @ curlauthentication.authenticateposturl(curlauthentication.java:109) @ curlauthentication.main(curlauthentication.java:134) error reading url
below code.
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.printwriter; import java.io.stringwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import javax.net.ssl.hostnameverifier; import javax.net.ssl.httpsurlconnection; import javax.net.ssl.sslsession; public class curlauthentication { public void authenticateposturl() { hostnameverifier hv = new hostnameverifier() { @override public boolean verify(string urlhostname, sslsession session) { system.out.println("warning: url host: " + urlhostname + " vs. " + session.getpeerhost()); return true; } }; // telling jre trust https server. // if know url connecting should // not problem try { trustallhttpscertificates(); } catch (exception e) { system.out.println("trustall" + e.getstacktrace()); } httpsurlconnection.setdefaulthostnameverifier(hv); stringwriter sw = new stringwriter(); printwriter pw = new printwriter(sw); try { url url = new url("www.stackoverflow.com"); string credentials = "user" + ":" + "password"; string encoding = base64converter.encode(credentials.getbytes("utf-8")); httpsurlconnection uc = (httpsurlconnection) url.openconnection(); uc.setdoinput(true); uc.setdooutput(true); uc.setrequestproperty("authorization", string.format("basic %s", encoding)); uc.setrequestmethod("post"); uc.setrequestproperty("content-type", "application/xml"); uc.setrequestproperty("accept", "application/xml"); uc.getinputstream(); system.out.println(uc.getcontenttype()); inputstream content = (inputstream) uc.getinputstream(); bufferedreader in = new bufferedreader(new inputstreamreader( content)); string line; while ((line = in.readline()) != null) { pw.println(line); } } catch (malformedurlexception e) { e.printstacktrace(); pw.println("invalid url"); } catch (ioexception e) { e.printstacktrace(); pw.println("error reading url"); } catch (exception e) { e.printstacktrace(); } system.out.println(sw.tostring()); } public static void main(string[] args) { // todo auto-generated method stub curlauthentication au = new curlauthentication(); au.authenticateposturl(); } // add these 2 functions in program public static class temptrustedmanager implements javax.net.ssl.trustmanager, javax.net.ssl.x509trustmanager { public java.security.cert.x509certificate[] getacceptedissuers() { return null; } public boolean isservertrusted( java.security.cert.x509certificate[] certs) { return true; } public boolean isclienttrusted( java.security.cert.x509certificate[] certs) { return true; } public void checkservertrusted( java.security.cert.x509certificate[] certs, string authtype) throws java.security.cert.certificateexception { return; } public void checkclienttrusted( java.security.cert.x509certificate[] certs, string authtype) throws java.security.cert.certificateexception { return; } } private static void trustallhttpscertificates() throws exception { // create trust manager not validate certificate chains: javax.net.ssl.trustmanager[] trustallcerts = new javax.net.ssl.trustmanager[1]; javax.net.ssl.trustmanager tm = new temptrustedmanager(); trustallcerts[0] = tm; javax.net.ssl.sslcontext sc = javax.net.ssl.sslcontext.getinstance("ssl"); sc.init(null, trustallcerts, null); javax.net.ssl.httpsurlconnection.setdefaultsslsocketfactory( sc.getsocketfactory()); } }
do iam doing wrong above code? need use curl script post data?
well, error 415
stating:
415 unsupported media type
the request entity has media type server or resource not support. example, client uploads image image/svg+xml, server requires images use different format.
without knowing specifications of server expecting of send him, it's hard tell missing. though seems if want receive data server , not send him anything.
by setting uc.setrequestproperty("content-type", "application/xml");
tell server hand him over xml data (which don't) , wouldn't expect this, he's giving error.
as sending urlencoded data in post data, try setting to:
uc.setrequestproperty("content-type", "application/x-www-form-urlencoded; charset=utf-8");
Comments
Post a Comment