Search in sources :

Example 21 with Cookie

use of okhttp3.Cookie in project AntennaPod by AntennaPod.

the class AntennapodHttpClient method newBuilder.

/**
     * Creates a new HTTP client.  Most users should just use
     * getHttpClient() to get the standard AntennaPod client,
     * but sometimes it's necessary for others to have their own
     * copy so that the clients don't share state.
     * @return http client
     */
@NonNull
public static OkHttpClient.Builder newBuilder() {
    Log.d(TAG, "Creating new instance of HTTP client");
    System.setProperty("http.maxConnections", String.valueOf(MAX_CONNECTIONS));
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    // detect 301 Moved permanently and 308 Permanent Redirect
    builder.networkInterceptors().add(chain -> {
        Request request = chain.request();
        Response response = chain.proceed(request);
        if (response.code() == HttpURLConnection.HTTP_MOVED_PERM || response.code() == StatusLine.HTTP_PERM_REDIRECT) {
            String location = response.header("Location");
            if (location.startsWith("/")) {
                HttpUrl url = request.url();
                location = url.scheme() + "://" + url.host() + location;
            } else if (!location.toLowerCase().startsWith("http://") && !location.toLowerCase().startsWith("https://")) {
                HttpUrl url = request.url();
                String path = url.encodedPath();
                String newPath = path.substring(0, path.lastIndexOf("/") + 1) + location;
                location = url.scheme() + "://" + url.host() + newPath;
            }
            try {
                DBWriter.updateFeedDownloadURL(request.url().toString(), location).get();
            } catch (Exception e) {
                Log.e(TAG, Log.getStackTraceString(e));
            }
        }
        return response;
    });
    // set cookie handler
    CookieManager cm = new CookieManager();
    cm.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER);
    builder.cookieJar(new JavaNetCookieJar(cm));
    // set timeouts
    builder.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
    builder.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
    builder.writeTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS);
    // configure redirects
    builder.followRedirects(true);
    builder.followSslRedirects(true);
    ProxyConfig config = UserPreferences.getProxyConfig();
    if (config.type != Proxy.Type.DIRECT) {
        int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
        SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
        Proxy proxy = new Proxy(config.type, address);
        builder.proxy(proxy);
        if (!TextUtils.isEmpty(config.username)) {
            String credentials = Credentials.basic(config.username, config.password);
            builder.interceptors().add(chain -> {
                Request request = chain.request().newBuilder().header("Proxy-Authorization", credentials).build();
                return chain.proceed(request);
            });
        }
    }
    if (16 <= Build.VERSION.SDK_INT && Build.VERSION.SDK_INT < 21) {
        builder.sslSocketFactory(new CustomSslSocketFactory(), trustManager());
    }
    return builder;
}
Also used : JavaNetCookieJar(okhttp3.JavaNetCookieJar) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) HttpUrl(okhttp3.HttpUrl) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) Response(okhttp3.Response) Proxy(java.net.Proxy) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) CookieManager(java.net.CookieManager) NonNull(android.support.annotation.NonNull)

Example 22 with Cookie

use of okhttp3.Cookie in project Gradle-demo by Arisono.

the class testUASApi method saveFormData.

/**
	 * 动态表单保存 接口
	 */
public static void saveFormData() {
    String url = baseurl + "/mobile/oa/commonSaveAndSubmit.action";
    OkhttpUtils.println(url);
    RequestBody formBody = new FormBody.Builder().add("master", master).add("gridStore", "null").add("formStore", "{\"va_holidaytype\":\"按小时\",\"va_vacationtype\":\"事假\",\"va_status\":\"在录入\",\"va_emcode\":\"sunquan\",\"va_emname\":\"龚鹏明\",\"va_department\":\"测试\",\"va_position\":\"测试\",\"va_mankind\":\"副总及以上\",\"va_alldays\":\"12\",\"va_alltimes\":\"25\",\"va_startime\":\"2016-11-28 13:39:00\",\"va_remark\":\"测试\",\"va_recordor\":\"刘佳\",\"va_date\":\"2016-11-28 13:39:00\",\"va_endtime\":\"2016-12-28 13:39:00\"}").add("caller", "Ask4Leave").add("sessionId", sessionId).build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            OkhttpUtils.println(OkhttpUtils.getResponseString(response));
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 23 with Cookie

use of okhttp3.Cookie in project Gradle-demo by Arisono.

the class testUASApi method getDBFindData.

/**
	 * @category 多选接口
	 */
public static void getDBFindData() {
    String url = baseurl + "common/dbfind.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("which", "form").add("pageSize", "5").add("condition", "1=1").add("field", "va_emname").add("caller", "Ask4Leave").add("sessionId", sessionId).add("page", "1").build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String json = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(json);
            String dataStr = JSON.parseObject(json).getString("data");
            // values
            JSONArray datas = JSON.parseArray(dataStr);
            System.out.println(datas.getJSONObject(0).getString("em_name"));
            JSONArray dbfinds = JSON.parseObject(json).getJSONArray("dbfinds");
            String fieldkey = "";
            for (int i = 0; i < dbfinds.size(); i++) {
                JSONObject item = dbfinds.getJSONObject(i);
                String key = item.getString("field");
                if (key.equals("va_emname")) {
                    System.out.println(item.getString("dbGridField"));
                    fieldkey = item.getString("dbGridField");
                }
            }
            System.out.println("url:" + url);
            System.out.println("master:" + master);
            System.out.println("emcode:" + emcode);
            for (int i = 0; i < datas.size(); i++) {
                System.out.println("value" + i + ":" + datas.getJSONObject(i).getString(fieldkey));
            }
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Call(okhttp3.Call) FormBody(okhttp3.FormBody) Request(okhttp3.Request) JSONArray(com.alibaba.fastjson.JSONArray) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) JSONObject(com.alibaba.fastjson.JSONObject) RequestBody(okhttp3.RequestBody)

Example 24 with Cookie

use of okhttp3.Cookie in project Gradle-demo by Arisono.

the class testUASApi method getStringCode.

public static void getStringCode(String method) {
    //master=USOFTSYS, sessionUser=U0316, sessionId=29DB60DE6E40D859B9169FE5013A8520, caller=CardLog, type=2
    String url = baseurl + "common/getCodeString.action";
    RequestBody formBody = new FormBody.Builder().add("master", master).add("type", "2").add("caller", "CardLog").add("sessionId", sessionId).build();
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").post(formBody).build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String result = OkhttpUtils.getResponseString(response);
            OkhttpUtils.println(result, OkhttpUtils.typeMiddle, method);
            code = JSON.parseObject(result).getString("code");
            saveCardLog("12.12", code, "saveCardLog");
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) FormBody(okhttp3.FormBody) Request(okhttp3.Request) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 25 with Cookie

use of okhttp3.Cookie in project Gradle-demo by Arisono.

the class testUASApi method getFormandGridDetail.

/**
	 * 接口 获取动态表单数据--请假单
	 * 
	 * @throws UnsupportedEncodingException
	 *             Workovertime Ask4Leave
	 */
public static void getFormandGridDetail(final String caller) {
    String url = baseurl + "mobile/getformandgriddetail.action?caller=" + caller + "&condition=1=1" + "&sessionId=" + sessionId + "&id=-1";
    OkhttpUtils.println(url);
    Request request = new Request.Builder().url(url).header("cookie", "JSESSIONID=" + sessionId).addHeader("sessionUser", emcode).addHeader("content-type", "text/html;charset:utf-8").build();
    OkhttpUtils.client.newCall(request).enqueue(new Callback() {

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            OkhttpUtils.println(caller + ":" + OkhttpUtils.getResponseString(response));
        }

        @Override
        public void onFailure(Call call, IOException e) {
            OkhttpUtils.onFailurePrintln(e);
        }
    });
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) Callback(okhttp3.Callback) Request(okhttp3.Request) IOException(java.io.IOException)

Aggregations

Response (okhttp3.Response)34 IOException (java.io.IOException)33 Request (okhttp3.Request)33 Call (okhttp3.Call)25 Callback (okhttp3.Callback)22 RequestBody (okhttp3.RequestBody)21 Test (org.junit.Test)18 Cookie (okhttp3.Cookie)17 MockResponse (okhttp3.mockwebserver.MockResponse)16 FormBody (okhttp3.FormBody)12 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)10 CookieManager (java.net.CookieManager)9 HttpCookie (java.net.HttpCookie)9 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 OkHttpClient (okhttp3.OkHttpClient)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 HttpUrl (okhttp3.HttpUrl)5 SharedPreferences (android.content.SharedPreferences)4 OnClick (butterknife.OnClick)4