Can't get JSOn Response when I POST to

1143
2
03-09-2018 12:12 PM
SteveOchieng
New Contributor III

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 = new DataOutputStream(httpConnection.getOutputStream());
wr.write(bytesArray);
wr.flush();
wr.close();

InputStream in = httpConnection.getInputStream();
Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
StringBuilder content = new StringBuilder();
char[] buffer = new char[5000];
int n;
while ( ( n = reader.read(buffer)) != -1 ) {
    content.append(buffer, 0, n);
}
reader.close();
return content.toString();

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 SCILLIERIDevSummit Conference

Help please!

0 Kudos
2 Replies
RandyBurton
MVP Alum

I believe the request should go to: https://www.arcgis.com/sharing/rest/generateToken (word "rest" between sharing and generateToken).

0 Kudos
SteveOchieng
New Contributor III

Actually this one  https://www.arcgis.com/sharing/generateToken  worked okay , maybe they are replaceable.

The things I changed is on

wr.writebytes(stringParams);
0 Kudos