java - Https PUT request with user name and password which send the JSON file as a payload -
i new java. have url, username, password. need make put http request using url, username , password. have send json file payload using http put request system can accept json.
i have searched in stackoverflow , found few suggestions can make simple request url without user name , password. our url need authenticated make request.
code have used make http request
public static void main (string args[]) throws ioexception { string userpassword = ""; string encoding = new string(base64.encodebase64(userpassword.getbytes())); url url = new url(url); httpurlconnection httpcon = (httpurlconnection) url.openconnection(); httpcon.setdooutput(true); httpcon.setrequestmethod("put"); httpcon.setrequestproperty("authorization", "basic "+encoding); outputstreamwriter out = new outputstreamwriter(httpcon.getoutputstream()); out.write("resource content"); out.close(); httpcon.getinputstream(); }
use volley library..its easy
requestqueue queue = volley.newrequestqueue(this); string url = "<your url>"; jsonobject jsonrequest=new jsonobject(); jsonrequest.put("username",<username>); jsonrequest.put("password",<password>); jsonobjectrequest jsobjrequest = new jsonobjectrequest(request.method.post, url, jsonrequest, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { // todo auto-generated method stub //your code handle when response came } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // todo auto-generated method stub } }); queue.add(jsobjrequest);
Comments
Post a Comment