日期:2014-05-20  浏览次数:20739 次

JAVA 如何实现HTTP的POST方式通讯,以及HTTPS方式传递
有一个A系统,使用WebService接口方式提供远程服务,我们现在想向这个接口发送XML文件

第一种方式:
1)HTTP的POST方式向A系统发送XML文件,

2)HTTPS方式向A系统发送XML文件,

请问如何实现,着急,有分哦!
知道的请与我联系:
邮箱:xiaxiaorui2003@163.com

给个例子的代码最好,呵呵

------解决方案--------------------
一个建议, 你直接把 xml 文件 放到字符串中,然后把这个字符串放到一个 input 中, 直接 用 httpclient post 过去就行了
不必要传文件
------解决方案--------------------

我通过 c# 把 要传递的内容传到服务器上的

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(RequestAddress);
req.Method = "POST";
req.KeepAlive = false;
req.ContentType = "application/x-www-form-urlencoded;charset=" + Encoding.ToString();
req.ContentLength = postBytes.Length;

你看看对你有没有用处,
这里面少了 参数的设置
------解决方案--------------------
用httpClientl来通信。

具体例子很多啊。baidu一下

google现在不好用了。被D封了。
------解决方案--------------------
private static void processData() {

  
try{
// HttpClient httpClient = new HttpClient();
String StrSoapMsg="<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://WebXml.com.cn/\">"
+"<soap:Header/>"
+"<soap:Body>"
+"<web:getWeatherbyCityName>"
+"<web:theCityName>beijing</web:theCityName>"
+"</web:getWeatherbyCityName>"
+"</soap:Body>"
+"</soap:Envelope>";
String Url = strUrlService;
URL url = new URL(Url);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");//这里根据服务器情况看 一种是//application/soap+xml 还有一种是text/xml

httpConn.setRequestProperty("SOAPAction", "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
OutputStream out = httpConn.getOutputStream();
Writer wout = new OutputStreamWriter(out);
wout.write(StrSoapMsg.trim());
wout.flush();
wout.close();
 
 
/* BufferedReader rd = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.print( line); 

}*/
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
char[] ok = new char[1024*2] ;
String strRead = "";
int i = 0 ;
while (true){
i = i+1;
if (i == iGetStrLong){
break ;
}
if (isr.read(ok)== -1){
strRead = strRead + (new String(ok)).trim() ;
break;
}
else{
strRead = strRead + (new String(ok)).trim() ;
ok = null;
ok = new char[1024*20] ;
}
}
strRead = strRead.trim();

isr.close();
 
System.out.print(strRead.toString());
 
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
  
  
}
 
}


结贴吧 发送返回全给你写了 你只需要把发送的xml换下就行了