I'm trying to make a HttpUrlConnection POST to the PayPal sandbox token to get an access token, but when I run my app it ignores the entered values (method, doOutput...).
I tried HttpsUrlConnection too.
Where is the problem?
This method is called from the AsyncTask class, inside the doInBackground() method.
private String makeHttpAuth(URL authURL) throws IOException, JSONException {
// authUrl = "https://api.sandbox.paypal.com/v1/oauth2/token"
// BASICINPUT = "Basic " + Base64.encodeToString(AUTHINPUT.getBytes(), Base64.NO_WRAP);
String authJsonResponse = "";
String grant = authBody().toString();
if (authURL == null) {
return authJsonResponse;
}
HttpURLConnection authentication = null;
InputStream inputStream = null;
try {
authentication = (HttpURLConnection) authURL.openConnection();
authentication.setRequestMethod("POST");
authentication.setConnectTimeout(15000);
authentication.setRequestProperty(AUTHORIZE, BASICINPUT);
authentication.setDoInput(true);
authentication.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(authentication.getOutputStream());
writer.write(grant);
writer.flush();
writer.close();
authentication.connect();
if (authentication.getResponseCode() == 200) {
inputStream = authentication.getInputStream();
authJsonResponse = readFromStream(inputStream);
} else {
Log.e(LOG_TAG, "Error response code: " + authentication.getResponseCode());
}
} finally {
if (authentication != null) {
authentication.disconnect();
}
if (inputStream != null) {
inputStream.close();
}
}
return authJsonResponse;
Expected:
...
method: "POST"
doOutput: true
responseCode: 200
...
Actual:
...
method: "GET"
doOutput: false
responseCode: -1
...
Related
The code below uses a protected url ,username password to get the files to download. I can only manage to download the file in the springboot folder. I want to send the file data to the frontend to have it download there to your downloads.
I might be wrong but I need to send the inputstream to the frontend, then download that data to a file? Any suggestions as to what I am doing wrong when trying to send this data to the frontend.
#RequestMapping(value = "/checkIfProtectedOrPublic/", method = RequestMethod.POST)
public ResponseEntity checkIfProtectedOrPublic(#RequestPart("prm_main") #Valid CheckProtectedData checkProtectedData) throws IOException {
List<PrmMain> prmMainList = prmMainRepository.findAllByCode("PROTECTED_LOGIN");
boolean success = true;
InputStream in = null;
FileOutputStream out = null;
for (int i = 0; i < prmMainList.size(); i++) {
if (prmMainList.get(i).getData().get("email").equals(checkProtectedData.getEmail())) {
String username= (String) prmMainList.get(i).getData().get("email");
String password= (String) prmMainList.get(i).getData().get("password");
try{
URL myUrl = new URL(checkProtectedData.getDownloadLink());
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setDoOutput(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept-Charset", "UTF-8");
conn.setRequestMethod("GET");
String userCredentials = username.trim() + ":" + password.trim();
String basicAuth = "Basic " + new String(Base64.encode(userCredentials.getBytes()));
conn.setRequestProperty ("Authorization", basicAuth);
in = conn.getInputStream();
out = new FileOutputStream(checkProtectedData.getFileName());
int c;
byte[] b = new byte[1024];
while ((c = in.read(b)) != -1){
out.write(b, 0, c);
}
}
catch (Exception ex) {
success = false;
}
finally {
if (in != null)
try {
in.close();
} catch (IOException e) {
}
if (out != null)
try {
out.close();
} catch (IOException e) {
}
}
}
}
return ResponseEntity.of(null);
}
//Complete redo of the code
PrmMain loginParameter = prmMainRepository.findAllByCode("PROTECTED_LOGIN").get(0);
if (loginParameter == null)
throw new IllegalArgumentException("Protected Login Not Configured");
// now try and download the file to a byte array using commons - this bypasses CORS requirements
HttpGet request = new HttpGet(checkProtectedData.getDownloadLink());
String login = String.valueOf(loginParameter.getData().get("email"));
String password = String.valueOf(loginParameter.getData().get("password"));
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(login, password));
try
(
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
CloseableHttpResponse response = httpClient.execute(request)
)
{
// if there was a failure send it
if (response.getStatusLine().getStatusCode() != HttpStatus.OK.value())
return new ResponseEntity<>(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
// send back the contents
HttpEntity entity = response.getEntity();
if (entity != null)
{
// return it as a String
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.parseMediaType(entity.getContentType().getValue()));
header.setContentLength(entity.getContentLength());
header.set("Content-Disposition", "attachment; filename=" + checkProtectedData.getFileName());
return new ResponseEntity<>(EntityUtils.toByteArray(entity), header, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
FRONTEND
export async function DownloadFile(url, request) {
axios({
url: `${localUrl + url}`, //your url
method: 'POST',
data: request,
responseType: 'blob', // important
}).then((response) =>
{
fileSaver.saveAs(new Blob([response.data]), request.fileName);
return true;
}).catch(function (error)
{
console.error('Failed ', error);
console.error('Failed ', error); console.log('Failed ', error);
}
);
}
I have a synchronous code based on httpURLConnection, and I have to convert it to the asynchronous mode.
Basic synchronous mode is fully functional, it works properly. To make it in the asynchronous way I am trying to use HttpClient with sendAsync method (JDK11). But I am stuck.
The summory of the project, as follows:
1) Several test classes, that extend abstractAPITest class.
2) This abstractAPITest class has the function of establishing the connection, and now I am working on it.
3) class User that invokes test classes
4) Main class that creates User instances in several threads and runs them.
Probably I should move the open connection function into User class or into main class? idk.
Here is what I have originally (sync):
byte[] sendRequest(JSONObject jsonObject, String username, String password) throws IOException, URISyntaxException {
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(this.apiUrl)).openConnection();
if (username != null && password != null) {
String userPassword = username + ":" + password;
httpURLConnection.setRequestProperty("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()));
}
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(jsonObject.toString().getBytes());
dataOutputStream.flush();
log.info("REST send: JSONObject");
if (httpURLConnection.getResponseCode() != 200) {
log.error("REST send error");
throw new IOException();
} else {
byte[] responseBody = null;
StringBuilder data = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
data.append(line);
responseBody = data.toString().getBytes();
}
br.close();
return responseBody;
}
}
and what I have as async:
byte[] request = jsonObject.toString().getBytes();
String userPassword;
if (username != null && password != null) {
userPassword = username + ":" + password;
} else {
throw new NullPointerException("No username and/or password.");
}
byte[] responseBody = null;
byte[] request = jsonObject.toString().getBytes();
var client = HttpClient.newHttpClient();
var httpRequest = HttpRequest.newBuilder()
.uri(new URI(apiUrl))
.version(HttpClient.Version.HTTP_2)
.header("Content-Type", "application/json")
.header("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()))
.POST(HttpRequest.BodyPublishers.ofByteArray(request))
.build();
HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString();
CompletableFuture<HttpResponse<String>> future = client.sendAsync(httpRequest, bodyHandler);
future.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
byte[] responseBody = null; // DON'T PAY ATTENTION
return responseBody; // TO THESE TWO LINES
}
private static String basicAuthorization(String username, String password) {
String userPassword = null;
if (username != null && password != null) {
userPassword = username + ":" + password;
}
return "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes());
}
So I need this async method do the same as sync method does, but asynchronously. So what can I do next?
I am trying to fetch the application only bearer token by using my consumer key and consumer secret following this. This is my implementation:
public class OAuthApplicationOnlyBearerTokenFetchTask extends AsyncTask<String, Void, String> {
private static Logger logger =
Logger.getLogger(OAuthApplicationOnlyBearerTokenFetchTask.class.getName());
final static String URL_TWITTER_OAUTH2_TOKEN = "https://api.twitter.com/oauth2/token";
final static String USER_AGENT = "TwitterMotion User Agent";
protected String mApplicationOnlyBearerToken;
#Override
protected String doInBackground(String... tokens) {
String consumerKey = tokens[0];
String consumerSecret = tokens[0];
String encodedCredentials = encodeKeysFrom(consumerKey, consumerSecret);
HttpURLConnection urlConnection = null;
try {
URL url = new URL(URL_TWITTER_OAUTH2_TOKEN);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Host", "api.twitter.com");
urlConnection.setRequestProperty("User-Agent", USER_AGENT);
urlConnection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
urlConnection.setRequestProperty("Content-Length", "29");
urlConnection.setUseCaches(false);
writeRequest(urlConnection, "grant_type=client_credentials");
String jsonResponse = readResponse(urlConnection);
logger.log(INFO, "jsonResponse of the bearer oauth request: ", jsonResponse);
if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_FORBIDDEN) {
logger.log(Level.SEVERE, "HTTP 403 (Forbidden) returned from Twitter API call for bearer token. " +
"Check values of Consumer Key and Consumer Secret in tokens.properties");
throw new RejectedAuthorizationException(urlConnection.getResponseCode(), "HTTP 403 (Forbidden) returned attempting to get Twitter API bearer token");
}
JSONObject jsonResponseObject = new JSONObject(jsonResponse);
if (jsonResponseObject != null) {
mApplicationOnlyBearerToken = (String) jsonResponseObject.get("access_token");
} else {
// TODO
}
return mApplicationOnlyBearerToken;
} catch (Exception ex) {
logger.log(Level.SEVERE, "", ex);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
#Override
protected void onPostExecute(String applicationOnlyBearerToken) {
this.mApplicationOnlyBearerToken = applicationOnlyBearerToken;
}
public String getApplicationOnlyBearerToken() {
return mApplicationOnlyBearerToken;
}
private String encodeKeysFrom(String consumerKey, String consumerSecret) {
try {
String encodedConsumerKey = URLEncoder.encode(consumerKey, "UTF-8");
String encodedConsumerSecret = URLEncoder.encode(consumerSecret, "UTF-8");
String combinedEncodedKey = encodedConsumerKey + ":" + encodedConsumerSecret;
byte[] encodedBytes = Base64.encode(combinedEncodedKey.getBytes(), Base64.NO_WRAP);
return new String(encodedBytes);
}
catch (UnsupportedEncodingException e) {
// TODO
return null;
}
}
private boolean writeRequest(HttpURLConnection connection, String requestBody)
throws IOException {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(
new OutputStreamWriter(connection.getOutputStream()));
bufferedWriter.write(requestBody);
bufferedWriter.flush();
return true;
}
catch (IOException ex) {
return false;
}
finally {
if (bufferedWriter != null) {
bufferedWriter.close();
}
}
}
private String readResponse(HttpURLConnection connection) throws IOException {
BufferedReader bufferedReader = null;
try {
StringBuilder stringBuilder = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + System.getProperty("line.separator"));
}
return stringBuilder.toString();
}
catch (IOException e) {
return null;
}
finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
}
}
But I am getting HTTP 403 Forbidden.
I also added permission on manifest file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
I can not understand what is the issue actually. Thanks in advance!
Never mind, I've found the bug.
String consumerKey = tokens[0];
String consumerSecret = tokens[0];
It should be
String consumerSecret = tokens[1];
I'm trying to send cookies to the server, but it doesn't work. Please tell me thats wrong. Here is my code.
At first, I take cookie in the POST request.
> Map <String, List<String>> headerFields = postRequest.getHeaderFields();
> List<String> cookiesHeader = headerFields.get("Set-Cookie");
Later, in the GET request, i'm send cookie to the server.
getRequest.setRequestProperty("Cookie", cookiesHeader.toString());
Help me. I'm beginner, not judge strictly.
Here all my code.
#Override
protected String doInBackground(Void... params) {
Log.i(TAG, "doInBackground");
String store_id = "";
final String COOKIES_HEADER = "Set-Cookie";
final String COOKIE = "Cookie";
try {
Thread.sleep(4000);
Log.i(TAG, "httpRequest start");
String parametrs = mPhone.getText().toString();
String parametrs2 = mPass.getText().toString();
JSONObject allParams = new JSONObject();
HttpURLConnection postRequest = null;
InputStream inputStream = null;
byte[] data = null;
try {
URL serverUrl = new URL("https://api.fianitlombard.ru/mobile/auth");
postRequest = (HttpURLConnection) serverUrl.openConnection();
postRequest.setReadTimeout(10000 /* milliseconds */);
postRequest.setConnectTimeout(15000 /* milliseconds */);
postRequest.setRequestMethod("POST");
postRequest.setDoInput(true);
postRequest.setDoOutput(true);
postRequest.setRequestProperty("Content-Type", "application/json; charset=utf-8");
postRequest.connect();
allParams.put("phone", parametrs);
allParams.put("password", parametrs2);
Log.i(TAG, "allParams" + allParams);
OutputStream bos = (postRequest.getOutputStream());
bos.write(allParams.toString().getBytes());
String helpInfo = postRequest.getResponseMessage();
Log.i(TAG, "helpInfo =" + helpInfo);
responseCode = postRequest.getResponseCode();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Map<String, List<String>> headerFields = postRequest.getHeaderFields();
List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
if (responseCode == 200) {
inputStream = postRequest.getInputStream();
byte[] buffer = new byte[8192]; // Такого вот размера буфер
// Далее, например, вот так читаем ответ
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = baos.toByteArray();
resultString = new String(data, "UTF-8");
Log.i(TAG, "responseCode = " + responseCode);
Log.i(TAG, "resultCode = " + resultString);
JSONObject jsonObject = new JSONObject(resultString);
store_id = jsonObject.getString("store_id");
Log.i(TAG, "store_id =" + store_id);
bos.close();
baos.close();
postRequest.disconnect();
}
if (responseCode == 403) {
Log.i(TAG, "responseCode = " + responseCode);
}
HttpURLConnection getRequest = null;
try {
URL serverUrl1 = new URL("https://api.fianitlombard.ru/mobile/checksession?version=1.0.8");
URI uri = URI.create("https://api.fianitlombard.ru/mobile/checksession?version=1.0.8");
getRequest = (HttpURLConnection) serverUrl1.openConnection();
getRequest.setReadTimeout(20000 /* milliseconds */);
getRequest.setConnectTimeout(25000 /* milliseconds */);
getRequest.setRequestMethod("GET");
getRequest.setRequestProperty("Content-Type", "application/json; charset=utf-8");
getRequest.setRequestProperty(COOKIE, cookiesHeader.toString());
Log.i(TAG, "Cookie = " + cookiesHeader.toString());
getRequest.connect();
int responceGetCode = getRequest.getResponseCode();
String responceGetInfo = getRequest.getResponseMessage();
Log.i(TAG, "responceGetCode = " + responceGetCode);
Log.i(TAG, "responceGetInfo = " + responceGetInfo);
if (responceGetCode == 200) {
//Все хорошо
}
if (responceGetCode == 400) {
// Устарела версия, нужно обновление
}
if (responceGetCode == 403) {
//Проблемы с авторизацией
} else {
//Что то другое.
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (getRequest != null)
getRequest.disconnect();
}
} catch (IOException e1) {
e1.printStackTrace();
}
if (postRequest != null) {
postRequest.disconnect();
}
Log.i(TAG, "httpRequest end");
}
catch (InterruptedException | JSONException e) {
e.printStackTrace();
}
return store_id;
}
change below line
getRequest.setRequestProperty(COOKIE, cookiesHeader.toString());
to
getRequest.setRequestProperty( COOKIE, cookiesHeader.get( 0 ) );
toString() method of List will return the hashCode() but not the actual values of List
try to use the following method to get the cookie :
String getHeaderField("Set-Cookie")
you set the cookie by using the lists toString method, which will not give you the current cookie representation, but instead a string matching "[var1, var2, var3]"
The server sends the following in its response header to set a cookie field.
Set-Cookie:name=value
If there is a cookie set, then the browser sends the following in its request header.
Cookie:name=value
See the HTTP Cookie article at Wikipedia for more information.
i'm trying to add the following headers to a rest Post call... it works in plain Java but i'm trying to re-write it using the Jersey client library... When I make the post with Jersey I get an error code which isn't listed in the API documentation so i know it must just be a small issue like a missing header... Any idea what i'm doing wrong in the bottom function?
Plain Java add headers function that works:
private void SetDefaultHeaders(HttpURLConnection conn) {
setRequestProperty(conn, "Accept", "*");
setRequestProperty(conn, "Content-Type", "application/x-www-form-urlencoded");
}
Jersey code:
public void logIn(String email, String password) {
if (email != "" && email != null && password != "" && password != null) {
try {
StringBuilder sb = new StringBuilder();
sb.append(Settings.WIFIPLUG_URL);
sb.append("/user_login");
MultivaluedMap<String, String> body = new MultivaluedMapImpl();
body.add("username=", email);
body.add("password=", password);
System.out.println("login url: " + sb.toString());
WebResource webResource = Client.create(new DefaultClientConfig()).resource(sb.toString());
WebResource.Builder builder = webResource.accept("*");
builder.type("application/x-www-form-urlencoded");
ClientResponse response = builder.post(ClientResponse.class, body);
if (response.getStatus() != 200) {
throw new RuntimeException("failed: http error code " + response.getStatus());
}
System.out.println("Response from server: " + response.getEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Full vanilla java login function:
public String postUserLogin(String username, String password) {
String result = "";
// URL for API to login
String url = "https://wifiplugapi.co.uk:3081/zcloud/api/user_login";
String requestParams = "username=" + username + "&password=" + password;
try {
URL obj = new URL(url);
System.out.println("login url: " + obj);
// Opens the connection
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
// Send POST request
con.setDoOutput(true);
con.setDoInput(true);
// Request Headers
con.setRequestMethod("POST");
// Sets all the headers
SetDefaultHeaders(con);
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
wr.write(requestParams);// adds values to the request
wr.flush();
wr.close();
// Handles the response
StringBuilder sb = new StringBuilder();
int responseCode = con.getResponseCode();
if (responseCode == 200) {
// if the request was successful OK = 200
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
// Returns Token
} else {
// If the request was bad, reason will be printed
result = "Error, login request failed";
throw new RuntimeException("Failed : HTTP error code : " + con.getResponseCode());
}
result = sb.toString();
// JSON Parser
JsonParser parser = new JsonParser();
JsonObject resultObj = parser.parse(result).getAsJsonObject();
con.disconnect();
if (resultObj.get("token") != null) {
result = (resultObj.get("token")).toString();
System.out.println("JSONObject Result (token): " + result);
} else {
System.out.println("result = " + result);
}
} catch (Exception e) {
e.printStackTrace();
}
// returns token value in string ie. fdg573gb3789gv923378gy83g3
result = result.replaceAll("\"", "");
return result;
}
You shouldn't have the = in the key when doing body.add. It will be added for you
MultivaluedMap<String, String> body = new MultivaluedMapImpl();
body.add("username=", email); // remove the =
body.add("password=", password); // remove the =