You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haojing/src/com/customization/dito/sendtodo/HttpRequestUtil.java

54 lines
1.4 KiB
Java

package com.customization.dito.sendtodo;
import com.sun.jersey.core.util.Base64;
import okhttp3.*;
import weaver.general.BaseBean;
import java.io.IOException;
public class HttpRequestUtil extends BaseBean{
/***
*
* @param dataJson
* @return
*/
public String doPostByAuth(String portal_todourl,String dataJson,String auth){
//"http://172.16.25.133/portal-web/centerTodo/sync"
BaseBean bb = new BaseBean();
String authorization = "Basic "+new String(Base64.encode(auth));
bb.writeLog("authorization:"+authorization);
bb.writeLog("portal_todourl:"+portal_todourl);
bb.writeLog("dataJson:"+dataJson);
bb.writeLog("auth:"+auth);
String msgData = "" ;
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, dataJson);
Request request = new Request.Builder()
.url(portal_todourl)
.method("POST", body)
.addHeader("Authorization", authorization)
.addHeader("Content-Type", "application/json")
.build();
try {
Response response = client.newCall(request).execute();
int code = response.code();
String bodyMsg = response.body().string();
bb.writeLog("response.code():"+code);
bb.writeLog("response.body():"+bodyMsg);
if(code == 200){
msgData = bodyMsg;
}
} catch (IOException e) {
e.printStackTrace();
}
bb.writeLog("msgData:"+msgData);
return msgData;
}
}