集册 Java实例教程 apache http客户端请求和响应处理程序

apache http客户端请求和响应处理程序

欢马劈雪     最近更新时间:2020-01-02 10:19:05

645
apache http客户端请求和响应处理程序


import java.io.IOException;
/*
时代Java
*/


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.HttpPut;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.util.EntityUtils;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;


public class UpdateBZ {


    public static void main(String[] args) throws JSONException,

            IOException {
/* 来 自 nowjava.com - 时  代  Java*/

        int idTicket = 13;


        JSONObject jso = new JSONObject();


        JSONArray jsaIdsValue = new JSONArray();

        jsaIdsValue.put(idTicket);


        String status = "CONFIRMED";


        String version = "autre chose";


        String resolution = "FIXED";

        JSONObject comment = new JSONObject();

        String bodyComment = "la raison de la modification";

        boolean isPrivate = false;

        comment.put("comment", bodyComment);

        comment.put("is_private", isPrivate);    

        jso.put("resolution", resolution);

        jso.put("comment", comment);


        jso.put("whiteboard", version);

        jso.put("ids", jsaIdsValue);


        HttpClient httpClient = HttpClientBuilder.create().build();

        HttpPut request = new HttpPut(

                "https://your server "

                        + idTicket

                        + "");

        StringEntity params = new StringEntity(jso.toString());

        request.addHeader("content-type", "application/json");

        request.addHeader("accept", "application/json");

        request.setEntity(params);


        // 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) {

展开阅读全文