如何使用HttpPost获取第二个响应

用户1687987

我正在使用 HttpPost 获取线程中的网络搜索结果。主要问题是第一个 httppost 请求的响应需要等待 web 处理。而且我不知道下一步该怎么做才能得到完整的响应结果。

请给我一些提示或想法。提前致谢。

问题响应图正确响应图

下面是我的源代码。

    try {
            String _url = "https://www.airfrance.com.tw/cgi-bin/AF/TW/zh/local/process/standardbooking/DisplayFlightPageAction.do";

            HttpPost httppost = new HttpPost(_url);
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
                String sessionID = response.getFirstHeader("Set-Cookie").getValue();
                cookiestore = httpclient.getCookieStore();
            }                
        } catch (Exception ex) {
        }

        try {
            DefaultHttpClient client = new DefaultHttpClient();

            // POST
            HttpPost post = new HttpPost(_url);

            client.setCookieStore(cookiestore);
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("typeTrip", "2"));
            params.add(new BasicNameValuePair("departure", "TPE"));
            params.add(new BasicNameValuePair("departure", "CDG"));
            params.add(new BasicNameValuePair("departureType", "AIRP"));
            params.add(new BasicNameValuePair("departureType", "AIRP"));
            params.add(new BasicNameValuePair("arrival", "CDG"));
            params.add(new BasicNameValuePair("arrival", "TPE"));
            params.add(new BasicNameValuePair("arrivalType", "AIRP"));
            params.add(new BasicNameValuePair("arrivalType", "AIRP"));
            params.add(new BasicNameValuePair("dayDate", "28"));
            params.add(new BasicNameValuePair("dayDate", "10"));
            params.add(new BasicNameValuePair("yearMonthDate", "201906"));
            params.add(new BasicNameValuePair("yearMonthDate", "201907"));
            params.add(new BasicNameValuePair("nbPassenger", "1"));
            params.add(new BasicNameValuePair("paxTypoList", "ADT"));
            params.add(new BasicNameValuePair("selectCabin", "on"));
            params.add(new BasicNameValuePair("plusOptions", ""));
            params.add(new BasicNameValuePair("nbAdults", "1"));
            params.add(new BasicNameValuePair("nbChildren", "0"));
            params.add(new BasicNameValuePair("nbEnfants", "0"));
            params.add(new BasicNameValuePair("nbBebes", "0"));
            params.add(new BasicNameValuePair("cabin", "Y"));
            params.add(new BasicNameValuePair("subCabin", "MCHER"));
            params.add(new BasicNameValuePair("haul", "LH"));
            params.add(new BasicNameValuePair("familyTrip", "NON"));
            params.add(new BasicNameValuePair("calendarSearch", "1"));
            params.add(new BasicNameValuePair("flyingBlueMember", "false"));
            params.add(new BasicNameValuePair("partnerRequest", "false"));
            params.add(new BasicNameValuePair("corporateMode", "false"));
            params.add(new BasicNameValuePair("isUM", "false"));
            params.add(new BasicNameValuePair("optionalUM", "false"));
            params.add(new BasicNameValuePair("mandatoryUM", "true"));
            params.add(new BasicNameValuePair("standardMandatory", "true"));
            params.add(new BasicNameValuePair("subscriberHOPContext", "false"));
            params.add(new BasicNameValuePair("hopCardOption", "NO_ANSWER"));
            params.add(new BasicNameValuePair("hopCardType", ""));
            params.add(new BasicNameValuePair("notFromFlight", "true"));


            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePOST = client.execute(post);

            int code = responsePOST.getStatusLine().getStatusCode();
            Log.e(mTAG, "web code = " + code);
            if (responsePOST.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntityPOST = responsePOST.getEntity();
                POSTresult = EntityUtils.toString(resEntityPOST);

            }
            mOTAFileThread = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
迈克尔·林

您可以将您的自定义ResponseHandlerexecute方法,这样你可以连里面你的第二个请求。

    // Create a custom response handler
    ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

        @Override
        public String handleResponse(
                final HttpResponse response) throws ClientProtocolException, IOException {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                // The response of first request is available here
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        }

    };
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println("----------------------------------------");
    System.out.println(responseBody);

参考:

ResponseHandler 官方示例

执行函数文档

除此之外,在android开发方面okhttp / Retrofit非常流行和强大HttpClient请随时查看。

OkHttp

改造

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用fgets获取第二个整数?

如何使用回调方法从服务获得第二个响应?

第二个函数如何获取数据?

Beautifulsoup findAll,如何获取第二个文本

使用jQuery获取tr的第二个td

使用条件获取第二个大数

使用jQuery获取第二个孩子

如何使用async / await获取第二个`then`回调参数?

如何使用 Javascript 获取表格行中的第二个复选框值

如何使用php从路径获取倒数第二个文件夹名称?

如何使用javascript或打字稿在表中获取tr的第二个td值

如何使用* pointer从函数中获取第二个值?

如何使用sed从第二个值获取输出

如何使用 AWS CLI 获取倒数第二个 AMI ID

如何使用Xpath选择第二个td?

如何使用UIPickerView设置第二个组件

如何使用循环调用第二个代码?

检查响应后,发送第二个响应

使用第二个元素

如何在第二个 SwitchMap 中返回 2 个响应对象并在订阅方法中使用

如何使用apigee中的ExtractVariables策略读取作为响应返回的xml项目列表的第二个元素?

如何根据第一个响应进行第二个API调用?

用管道grep响应第二个命令?

使用匿名类型时如何获取第二个或第三个XML节点?

如何添加额外的(第二个)属性并将其返回到sortable / nestedSortable响应?

如何解析JSON响应中的选择第二个HTML锚元素

如何在不使用循环的情况下获取每个子集的第二个元素-Python

如何在C#中使用XElement获取xml中第二个子元素的值

如何从两个表中获取数据,第二个表显示行数