xml格式:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
/**
* http 发送xml调用esb接口
*/
public class HttpPostXml {
/**
* 发送xml报文,调用接口
*
* @param url 接口服务地址
* @param reqXml 请求报文
* @return
*/
public static String postByXml(String url, String reqXml){
System.out.println("reqXml==================\n" + reqXml);
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
httpURLConnection.setRequestProperty("content-type", "text/html");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(5000);
httpURLConnection.setReadTimeout(5000);
httpURLConnection.connect();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
httpURLConnection.getOutputStream(), "UTF-8"));
out.write(reqXml);
out.flush();
int code = httpURLConnection.getResponseCode();
if (code != 200) {
System.out.println("ERROR===" + code);
}
// 打印返回参数
InputStream in = httpURLConnection.getInputStream();
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(in));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("resXml==================\n" + buffer.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void testRemoveBound(){
String custNo = "2216420645"; // 会员编码
StringBuffer reqXml = new StringBuffer();
reqXml.append("<?xml version='1.0' encoding='UTF-8'?>");
reqXml.append("<MbfService>");
reqXml.append("<input1>");
reqXml.append("<MbfHeader>");
reqXml.append("<ServiceCode>EPPBoundController</ServiceCode>");
reqXml.append("<Operation>remBoundOfEPPAcc</Operation>");
reqXml.append("<AppCode>SHP</AppCode>");
reqXml.append("<UId></UId>");
reqXml.append("<AuthId></AuthId>");
reqXml.append("</MbfHeader>");
reqXml.append("<MbfBody>");
reqXml.append("<custNo>"+custNo+"</custNo>");
reqXml.append("</MbfBody>");
reqXml.append("</input1>");
reqXml.append("</MbfService>");
String url = "";
url = "http://shppre.service.cnsuning.com:8080/shp-service-web/eppbound/removebound.do";
HttpPostXml.postByXml(url, reqXml.toString());
}
/**
* @param args
*/
public static void main(String[] args) {
testRemoveBound();
}
}
json格式:
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.springframework.http.HttpStatus;
import com.suning.rm.util.date.DateUtil;
/**
* 接口测试类
* (部分接口测试之前,需要将spring-servlet.xml里的权限拦截器配置注释掉)
*
* @author 14073034
*/
public class TestPostJson {
@SuppressWarnings("deprecation")
public static String post(String url, String json) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json);
s.setContentEncoding("UTF-8");
s.setContentType("application/json");
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
HttpEntity entity = res.getEntity();
String charset = EntityUtils.getContentCharSet(entity);
response = new JSONObject(new JSONTokener(
new InputStreamReader(entity.getContent(), charset)));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response.toString();
}
public static void getToken() throws UnsupportedEncodingException{
String employeeId = "14073034";
String pwd = "wTPfP+YFq4uag9AdC31gmi/zhjP4UZl8zZle24vcAAU6L/8BZGyehv2p0sAwWLHnETEd+q7MALkm0dgPCzQd86x45bNtmL2rg1se5v/doBfuGxkApE443MwCkzelX8iSMc/uzk2HIRb1Z65NUiWbobBwU0jQPTpDVcdFBK63270=";
String imei = "1234567890";
String serialNo = employeeId + DateUtil.getCurrentDateString(DateUtil.YYYYMMDDHHMMSSSSS);
JSONObject reqJsonObject = new JSONObject();
reqJsonObject.put("employeeId", employeeId);
reqJsonObject.put("imei", imei);
reqJsonObject.put("pwd", pwd);
reqJsonObject.put("serialNo", serialNo);
System.out.println("reqJson=" + reqJsonObject.toString());
String url = "";
url = "http://rmdev.cnsuning.com:8080/rm-web/service/getToken.htm";
String resJson = TestPostJson.post(url, URLEncoder.encode(reqJsonObject.toString(), "UTF-8"));
System.out.println("resJson=" + resJson);
}
public static void main(String[] args) throws UnsupportedEncodingException {
getToken();
}
}
GET方式:
public static void sendPost() {
String url = "http://yufulong.ittun.com/api.php";
String param = "src=SUNINGYOUPIN&method=Liangpin.productStatusNotify×tamp=1478501229&product_id=147848895336572642&sign=34216627b28afec2d23f3c0092d1ed26&pay_method=1&product_status=1";
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
System.out.println(result);
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}