java http 调用webservcie接口

					try {
				
						// 1 指定WebService服务的请求地址:
						String url="webserviceURL";
						// 2 创建URL:
						URL url1 = new URL(url);
						
						// 3 建立连接,并将连接强转为Http连接
						URLConnection conn = url1.openConnection();
						HttpURLConnection con = (HttpURLConnection) conn;

						// 4,设置请求方式和请求头:
						con.setDoInput(true); // 是否有入参
						con.setDoOutput(true); // 是否有出参
						con.setRequestMethod("POST"); // 设置请求方式
						con.setRequestProperty("content-type", "text/xml;charset=UTF-8");

						//设置请求参数
						String requestBody = "";
						requestBody += "";
						requestBody += "";
						
						
						requestBody +=""+json_data+"";
						
						requestBody += "";
						requestBody += "";
						requestBody += "";
						
						System.out.println(requestBody);
						// 6,通过流的方式将请求体发送出去:
						OutputStream out = con.getOutputStream();
						out.write(requestBody.getBytes("utf-8"));
						out.close();
						// 7,服务端返回正常:
						int code = con.getResponseCode();
						if (code == 200) {// 服务端返回正常
						InputStream is = con.getInputStream();
						byte[] b = new byte[1024];
						StringBuffer sb = new StringBuffer();
						int len = 0;
						while ((len = is.read(b)) != -1) {
						String str = new String(b, 0, len, "UTF-8");
						sb.append(str);
						}
			
						
						String result=sb.toString();
                        System.out.println(result);
						is.close();
						}
						con.disconnect();
						} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						}

 

你可能感兴趣的:(后台)