Saturday, 15 March 2014

web services - How to add header to HttpRequest of GET method in Java -


To use a valid web service to pass me a token, pass as part of the verification of each GET request Have to do. Below is the code that I am using to access it in the REST API:

  public static string httpGet (string URLSTR, string [] surname, string [] paramVal) throws exceptions { URL url = new URL (urlStr); HttpsURL Connection Connection = (HttpsURLConnection) url.openConnection (); Conn.setRequestMethod ("GET"); Conn.setDoOutput (right); Conn.setRequestProperty ("content-type", "app / x-www-form-urxed"); OutputStream Out = conn.getOutputStream (); Author Writer = New OutputStream Vector (outside, "UTF-8"); For (int i = 0; i & lt; paramName.length; i ++) {author; written (nomenclature [i]); Writer.write ("="); Author (written) (URLNcoder.xcode (Parwal [i], "UTF-8"); Writer.write ("& amp;"); } Author. Close (); Out.close (); If (conn.getResponseCode ()! = 200) {System.out.println ("Answer Code:" + conn.getResponseCode ()); Throw New IOException (conn.getResponseMessage ()); } BufferedReader rd = new BufferedReader (new InputStreamReader (conn.getInputStream ())); Stringbilder sb = new stringbiller (); String line; While ((line = rd.readline ()) = null) {sb.append (line); } Rd.close (); Conn.disconnect (); Return sb.toString (); }  

I can not see any such method to set the header conn.setHeader () HttpsURLConnection This is X-Cookie: token = {token}; , please help me find a way to set the header.

You can use:

  conn.addRequestProperty (" X-cookie "," token = {token} ");  

or setRequestProperty () also works


No comments:

Post a Comment