1 2 3 4 5 6 7 8 | < soap:Envelope xmlns:soap = "http://www.w3.org/2003/05/soap-envelope" xmlns:sam = "http://user.service.xxx.com" > < soap:Header /> < soap:Body > < sam:getUserInfo > < sam:userName >sunlightcs</ sam:userName > </ sam:getUserInfo > </ soap:Body > </ soap:Envelope > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentProducer; import org.apache.http.entity.EntityTemplate; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class ClientTest { public static void main(String[] args) { ClientTest.httpClientPost(); } private static void httpClientPost() { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://localhost:8080/xxx/services/userService" ); try { ContentProducer cp = new ContentProducer() { public void writeTo(OutputStream outstream) throws IOException { Writer writer = new OutputStreamWriter(outstream, "UTF-8" ); /** * 获取请求的xml格式数据 */ String requestXml = getRequestXml(); writer.write(requestXml); writer.flush(); } }; post.setEntity( new EntityTemplate(cp)); HttpResponse response = client.execute(post); //打印返回的xml数据 System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } private static String getRequestXml(){ StringBuilder sb = new StringBuilder( "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">" ); sb.append( "<soap:Header/>" ); sb.append( "<soap:Body>" ); sb.append( "<sam:getUserInfo>" ); sb.append( "<sam:userName>sunlightcs</sam:userName>" ); sb.append( "</sam:getUserInfo>" ); sb.append( "</soap:Body>" ); sb.append( "</soap:Envelope>" ); return sb.toString(); } } |
1 2 3 4 5 6 7 8 | <? xml version = '1.0' encoding = 'UTF-8' ?> < soapenv:Envelope xmlns:soapenv = "http://www.w3.org/2003/05/soap-envelope" > < soapenv:Body > < ns:getUserInfoResponse xmlns:ns = "http://user.service.xxx.com" > < ns:return >xxx</ ns:return > </ ns:getUserInfoResponse > </ soapenv:Body > </ soapenv:Envelope > |
联系客服