Http Post request with volley

huangapple go评论70阅读模式
英文:

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(&quot;aesKey&quot;);
iv = response.getString(&quot;aesIV&quot;);
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(&quot;deviceID&quot;, deviceID);
datas.put(&quot;systemVersion&quot;, Build.VERSION.SDK_INT);
datas.put(&quot;platformName&quot;,&quot;Android&quot;);
datas.put(&quot;deviceModel&quot;,Build.MODEL);
datas.put(&quot;manifacturer&quot;,Build.MANUFACTURER);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas, new Response.Listener&lt;JSONObject&gt;() {
@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&lt;String, String&gt; getHeaders() throws AuthFailureError {
Map&lt;String,String&gt; params = new HashMap&lt;&gt;();
params.put(&quot;Content-Type&quot;, &quot;application/json&quot;);
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(&quot;AES/CBC/PKCS7Padding&quot;);
SecretKeySpec keySpec = new SecretKeySpec(key, &quot;AES&quot;);
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(&quot;period&quot;, period);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, url, datas1, new Response.Listener&lt;JSONObject&gt;() {
@Override
public void onResponse(JSONObject response) {
try {
System.out.println(response.toString());
System.out.println(&quot;burası &#231;alıştı&quot;);
} 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&lt;String, String&gt; getHeaders() throws AuthFailureError {
Map&lt;String,String&gt; params = new HashMap&lt;&gt;();
params.put(&quot;Content-Type&quot;, &quot;application/json&quot;);
params.put(&quot;X-VP-Authorization&quot;, auth);
System.out.println(&quot;AUTH DEĞERİ &quot;+ auth);
return params;
}
};
queue.getCache().clear();
queue.add(req);
}

答案1

得分: 0

我有两个可能的答案:

  1. 你可能遇到了与标头本身有关的编码问题,需要在RFC-2616中进行验证,关于HTTP标准,标头字段名称必须是一个由一个或多个CHAR组成的令牌,CHAR = <任何US-ASCII字符(八位字节0-127)>
  2. 也许代理层中的一个(例如nginx或类似的代理)在请求发送到服务器之前“吃掉”了标头。也许你应该在接收发送400错误的服务器上验证它是否正确接收包含身份验证信息的标头。
英文:

I've got 2 possible answers

  1. 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)>
  2. 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.

huangapple
  • 本文由 发表于 2020年8月20日 06:32:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63495827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定