号簿助手 v4.6.31版本的 MD5 值为:9d7ff98a6bb5cc178eac2df4d1b2f0e0

以下内容为反编译后的 AudHttpPost.java 源代码,内容仅作参考


package com.sttri.speech.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

public final class AudHttpPost {
    public static final String BOUNDARY = "--ASR++LIB";
    public static final String CONTENT_TYPE = "multipart/mixed;boundary=--ASR++LIB";
    public static final String LINE_END = "\r\n";
    public static final String PREFIX = "--";
    public static final String TAG = "AudHttpPost";
    private byte[] block;
    private HttpClient httpClient;
    private String jsonStr;
    private int postBufferLen;
    private String url;
    public static final byte[] FIRST_BOUNDARY = "----ASR++LIB\r\n".getBytes();
    public static final byte[] MIDDLE_BOUNDARY = "\r\n----ASR++LIB\r\n".getBytes();
    public static final byte[] LAST_BOUNDARY = "\r\n----ASR++LIB--".getBytes();
    private static BasicHttpParams httpParams = new BasicHttpParams();
    private long requestTime = 1;
    private int statusCode = 0;
    ResponseHandler<String> handler = new ResponseHandler<String>() {
        @Override
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            AudHttpPost.this.statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity, "UTF-8");
            }
            return null;
        }
    };

    static {
        HttpConnectionParams.setConnectionTimeout(httpParams, 20000);
        HttpConnectionParams.setSoTimeout(httpParams, 30000);
        HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
        HttpClientParams.setRedirecting(httpParams, true);
        HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
        HttpProtocolParams.setUserAgent(httpParams, "CTTR");
        HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
    }

    public AudHttpPost(String url, HttpClient httpClient) {
        this.url = "http://192.168.2.166:80/nlpp/regno2";
        this.url = url;
        this.httpClient = httpClient;
    }

    public void setData(String jsonStr, byte[] block, int postBufferLen) {
        this.block = block;
        this.jsonStr = jsonStr;
        this.postBufferLen = postBufferLen;
    }

    private HttpUriRequest readyRequest(String url, String jsonStr, byte[] bin_data, int postBufferLen) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(FIRST_BOUNDARY);
            addJsonPart(jsonStr, baos);
            baos.write(MIDDLE_BOUNDARY);
            addBinPart(bin_data, postBufferLen, "CT:BV32/L16;rate=16000", true, baos);
            baos.write(LAST_BOUNDARY);
            ByteArrayEntity bae = new ByteArrayEntity(baos.toByteArray());
            bae.setContentType(CONTENT_TYPE);
            HttpPost post = new HttpPost(url);
            post.setEntity(bae);
            return post;
        } catch (Exception e) {
            throw new IllegalArgumentException("can't create HttpCallable instance", e);
        }
    }

    private void addJsonPart(String jsonStr, ByteArrayOutputStream output) {
        try {
            byte[] byteJson = jsonStr.getBytes("utf-8");
            output.write(("Content-Disposition:form-data; name=\"meta\"\r\n\r\n").getBytes());
            output.write(byteJson);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void addBinPart(byte[] data, int postBufferLen, String content_type, boolean hasVoiceHead, ByteArrayOutputStream output) {
        StringBuilder partHeader = new StringBuilder();
        partHeader.append("Content-Disposition: form-data; name=\"audio\"");
        partHeader.append("\r\n");
        partHeader.append("HAS-V-HEAD:1");
        partHeader.append("\r\n");
        partHeader.append(content_type);
        partHeader.append("\r\n");
        partHeader.append("CL:" + postBufferLen);
        partHeader.append("\r\n");
        partHeader.append("\r\n");
        try {
            output.write(partHeader.toString().getBytes());
            output.write(data, 0, postBufferLen);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean isReady() {
        return this.requestTime > 0 && System.currentTimeMillis() - this.requestTime > 30;
    }

    public String call() throws Exception {
        HttpUriRequest httpRequest = readyRequest(this.url, this.jsonStr, this.block, this.postBufferLen);
        String content = (String) this.httpClient.execute(httpRequest, this.handler);
        return content;
    }

    public int getStatusCode() {
        return this.statusCode;
    }

    private static byte[] gZip(byte[] data) {
        byte[] b = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(bos);
            gzip.write(data);
            gzip.finish();
            gzip.close();
            b = bos.toByteArray();
            bos.close();
            return b;
        } catch (Exception ex) {
            ex.printStackTrace();
            return b;
        }
    }
}