I'm using Java HttpURLConnection to send a POST request to https://www.arcgis.com/sharing/generateToken with the parameters username, password, f, expiration, referer.
I need to get back the JSON response to extract the Token and store in my object, but the result I get seemingly is an HTML Page.
This is what I' sing, I simplified the code to keep my question clean
DataOutputStream wr <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">DataOutputStream</SPAN><SPAN class="punctuation token">(</SPAN>httpConnection<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getOutputStream</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
wr<SPAN class="punctuation token">.</SPAN><SPAN class="token function">write</SPAN><SPAN class="punctuation token">(</SPAN>bytesArray<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
wr<SPAN class="punctuation token">.</SPAN><SPAN class="token function">flush</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
wr<SPAN class="punctuation token">.</SPAN><SPAN class="token function">close</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
InputStream in <SPAN class="operator token">=</SPAN> httpConnection<SPAN class="punctuation token">.</SPAN><SPAN class="token function">getInputStream</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
Reader reader <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">BufferedReader</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">InputStreamReader</SPAN><SPAN class="punctuation token">(</SPAN>in<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"UTF-8"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
StringBuilder content <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">StringBuilder</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">char</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN> buffer <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">char</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">5000</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">int</SPAN> n<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN> <SPAN class="punctuation token">(</SPAN> n <SPAN class="operator token">=</SPAN> reader<SPAN class="punctuation token">.</SPAN><SPAN class="token function">read</SPAN><SPAN class="punctuation token">(</SPAN>buffer<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">!=</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
content<SPAN class="punctuation token">.</SPAN><SPAN class="token function">append</SPAN><SPAN class="punctuation token">(</SPAN>buffer<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> n<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
reader<SPAN class="punctuation token">.</SPAN><SPAN class="token function">close</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">return</SPAN> content<SPAN class="punctuation token">.</SPAN><SPAN class="token function">toString</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Initial Params string: username=<correct username>&passowrd=<correct password>&f=json&expiration=60&referer=http%3Aww.arcgis.com
Data Bytes: [B@7506e922Sending POST request to URL : https://www.arcgis.com/sharing/generateToken
This is the response I get back.
Status Code:200
JSON Response Excpected: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>ArcGIS Portal Directory</title><link href="https://community.esri.com/sharing/files/gw.css" rel="stylesheet" type="text/css"/></head><body><br/><div class="gwDiv"><table class="navTable" width="100%"><tr><td class="breadcrumbs">ArcGIS Portal Directory</td></tr></table><tr valign="top"><br/><br/><b><a href="http://resources.arcgis.com/en/help/arcgis-rest-api/" >API Reference</a></b></tr></table></div></body></html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>ArcGIS Portal Directory</title><link href="https://community.esri.com/sharing/files/gw.css" rel="stylesheet" type="text/css"/></head><body><br/><div class="gwDiv"><table class="navTable" width="100%"><tr><td class="breadcrumbs">ArcGIS Portal Directory</td></tr></table><tr valign="top"><br/><br/><b><a href="http://resources.arcgis.com/en/help/arcgis-rest-api/" >API Reference</a></b></tr></table></div></body></html>
Process finished with exit code 0
How do I get the JSON so that I can store the token in an object in Java Christophe SCILLIERI DevSummit Conference
Help please!