android - how to add token in httpurlconnection class -


i using code api access.but have no idea how add token header in httpurlconnection class.my tag token "token".i want add "token" tag token value in header field have no idea.i have googled confused

  public jsonobject makehttprequest(string murl, utils.method m, map<string, string> parmas, string token, string filepath, string filefield, string filemimetype){     httpurlconnection connection = null;     dataoutputstream outputstream = null;     inputstream inputstream = null;      string twohyphens = "--";     string boundary = "*****" + long.tostring(system.currenttimemillis()) + "*****";     string lineend = "\r\n";     string urlto = server_path + murl;      string result = "";      int bytesread, bytesavailable, buffersize;     byte[] buffer;     int maxbuffersize = 1 * 1024 * 1024;      try {         url url = new url(urlto);         connection = (httpurlconnection) url.openconnection();          connection.setdoinput(true);         connection.setdooutput(true);         connection.setusecaches(false);          connection.setrequestmethod("post");         connection.setrequestproperty("connection", "keep-alive");   //            connection.setrequestproperty("user-agent", "android multipart http client 1.0");         connection.setrequestproperty("cache-control", "no-cache");         connection.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary);         if (token != null){             connection.setrequestproperty("token", "basic " + new string(base64.encode(token.getbytes(), 0)));         }          outputstream = new dataoutputstream(connection.getoutputstream());         outputstream.writebytes(twohyphens + boundary + lineend);         if (filepath != null) {             string[] q = filepath.split("/");             int idx = q.length - 1;             file file = new file(filepath);             fileinputstream fileinputstream = new fileinputstream(file);             outputstream.writebytes("content-disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineend);             outputstream.writebytes("content-type: " + filemimetype + lineend);             outputstream.writebytes("content-transfer-encoding: binary" + lineend);             bytesavailable = fileinputstream.available();             buffersize = math.min(bytesavailable, maxbuffersize);             buffer = new byte[buffersize];              bytesread = fileinputstream.read(buffer, 0, buffersize);             while (bytesread > 0) {                 outputstream.write(buffer, 0, buffersize);                 bytesavailable = fileinputstream.available();                 buffersize = math.min(bytesavailable, maxbuffersize);                 bytesread = fileinputstream.read(buffer, 0, buffersize);             }              outputstream.writebytes(lineend);         }         //            outputstream.writebytes(getquery(parmas));         iterator<string> keys = parmas.keyset().iterator();         while (keys.hasnext()) {             string key = keys.next();             string value = parmas.get(key);             system.out.println("key :"+key+" , value :"+value);             outputstream.writebytes(twohyphens + boundary + lineend);             outputstream.writebytes("content-disposition: form-data; name=\"" + key + "\"" + lineend);             outputstream.writebytes("content-type: text/plain" + lineend);             outputstream.writebytes(lineend);             outputstream.writebytes(value);             outputstream.writebytes(lineend);         }         outputstream.writebytes(twohyphens + boundary + twohyphens + lineend);         system.out.println("response code :"+connection.getresponsecode());         if (200 != connection.getresponsecode()) {          }          inputstream = connection.getinputstream();          result = this.convertstreamtostring(inputstream);         jobj = new jsonobject(result);         inputstream.close();         outputstream.flush();         outputstream.close();         connection.disconnect();      } catch (filenotfoundexception e) {         e.printstacktrace();     } catch (malformedurlexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     } catch (jsonexception e) {         e.printstacktrace();     }      return jobj; } 


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -