英文:
Http Post request with volley
问题
以下是您提供的代码部分的中文翻译:
// 这是解码 Base64 格式的 aeskey 和 aesiv 的方法
String key = response.getString("aesKey");
String iv = response.getString("aesIV");
byte[] aeskey = Base64.decode(key, Base64.DEFAULT);
byte[] aesiv = Base64.decode(iv, Base64.DEFAULT);
// 这是第一个请求的代码
public void getData(String url, String deviceID) throws JSONException {
JSONObject datas = new JSONObject();
datas.put("deviceID", deviceID);
datas.put("systemVersion", Build.VERSION.SDK_INT);
datas.put("platformName", "Android");
datas.put("deviceModel", Build.MODEL);
datas.put("manifacturer", Build.MANUFACTURER);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
return params;
}
};
queue.getCache().clear();
queue.add(req);
}
// 这是加密方法
public static byte[] encrypt(String plainText, byte[] key, byte[] iv) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, paramSpec);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
return cipherText;
}
// 这是第二个请求的代码
public void getanotherdata(String url, String period, final String auth) throws JSONException {
JSONObject datas1 = new JSONObject();
datas1.put("period", period);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas1,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.println(response.toString());
System.out.println("burası çalıştı");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
params.put("X-VP-Authorization", auth);
System.out.println("AUTH DEĞERİ " + auth);
return params;
}
};
queue.getCache().clear();
queue.add(req);
}
英文:
i am using volley to request to the server i have this url that i post my DeviceId, manufacturer, android version etc and i got a aeskey and aesiv base64 encoded and a Authorization key so then to another url i sent this Authorization key as a header and as a body i need to put "period(it can be = 'all' , 'increasing', 'decreasing' etc )" which is encrypted by the aeskey and aesiv with aes256 with padding PKCS7 and ECB or CBC. But i get response from the first request but from the second request i got http 400 error i dont know what am i doing wrong. I dont have the access to server so to headers and to the parameters i am just adding what i am told to. I am thinking maybe i am doing the encryption wrong ?
this is how i decode base64 aeskey and aesiv
key = response.getString("aesKey");
iv = response.getString("aesIV");
byte[] aeskey = Base64.decode(key, Base64.DEFAULT);
byte[] aesiv = Base64.decode(iv, Base64.DEFAULT);
this is my first request code
public void getData(String url,String deviceID) throws JSONException {
JSONObject datas = new JSONObject();
datas.put("deviceID", deviceID);
datas.put("systemVersion", Build.VERSION.SDK_INT);
datas.put("platformName","Android");
datas.put("deviceModel",Build.MODEL);
datas.put("manifacturer",Build.MANUFACTURER);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("Content-Type", "application/json");
return params;
}
};
queue.getCache().clear();
queue.add(req);
}
this is my encryption method
public static byte[] encrypt(String plainText, byte[] key, byte[] iv) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keySpec, paramSpec);
byte[] cipherText = cipher.doFinal(plainText.getBytes());
return cipherText;
}
this is my second method for second request
public void getanotherdata(String url, String period, final String auth) throws JSONException {
JSONObject datas1 = new JSONObject();
datas1.put("period", period);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas1, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.println(response.toString());
System.out.println("burası çalıştı");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("Content-Type", "application/json");
params.put("X-VP-Authorization", auth);
System.out.println("AUTH DEĞERİ "+ auth);
return params;
}
};
queue.getCache().clear();
queue.add(req);
}
答案1
得分: 0
我有两个可能的答案:
- 你可能遇到了与标头本身有关的编码问题,需要在RFC-2616中进行验证,关于HTTP标准,标头字段名称必须是一个由一个或多个CHAR组成的令牌,CHAR = <任何US-ASCII字符(八位字节0-127)>
- 也许代理层中的一个(例如nginx或类似的代理)在请求发送到服务器之前“吃掉”了标头。也许你应该在接收发送400错误的服务器上验证它是否正确接收包含身份验证信息的标头。
英文:
I've got 2 possible answers
- You may be facing an encoding issue with the header itself, to be verified in RFC-2616 about HTTP standard, the header field-name has to be a token that is made out of 1 or more CHAR = <any US-ASCII character (octets 0 - 127)>
- Maybe one of the proxy layer ( like nginx or similar ) is "eating" the header out of the requests before it gets to the server. Maybe you should verify on the receiving server that emits the 400 that it receives correclty the header that contains the authentication information.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论