import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hoyozero.qims.common.utils.CommonUtils;
import com.hoyozero.qims.common.utils.StringUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
@Component
public class HttpUtils {
/*@Autowired
private ISysHttpInterFaceLogService iSysHttpInterFaceLogService;*/
public static JSONObject putWithAuth(String url, JSONObject json, String user, String password) {
JSONObject result = new JSONObject();
result.put( "success", false );
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPut put = new HttpPut(url);
StringEntity myEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
if (user != null && password != null) {
String auth = user + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
put.addHeader("Authorization", authHeader);
}
put.setEntity(myEntity); // 设置请求体
String responseContent = "{}"; // 响应内容
CloseableHttpResponse response = client.execute(put);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
responseContent = EntityUtils.toString(entity, "UTF-8").replace("\\\\", "/");
}
response.close();
client.close();
result.put( "success", true );
result.put( "data", JSONObject.parseObject(responseContent));//JSONObject.fromObject(responseContent)
} catch (ClientProtocolException e) {
e.printStackTrace();
result.put("message", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
result.put("message", e.getMessage());
}
return result;
}
}
掉对外接口,xml转换
最新推荐文章于 2024-07-21 22:27:18 发布