2014-11-24 382 views
0

有一個.Net Web服務,我必須從我的本地應用程序發送XML數據。我的本地應用程序正在Java & Sql上運行。Web服務客戶端應用程序

Web服務正在接受xml類型。你能幫我嗎?我應該怎麼做?這種情況是否有例子?

+0

Google和Google你會發現大量的例子。不要指望當你可以做同樣的事情時,SO會爲你提供鏈接。請付出一些努力。 – SMA 2014-11-24 12:51:14

+0

我已經搜索谷歌,甚至YouTube教程。但我不明白。我應該怎麼做。因此,如果任何人在之前做過相同的申請,他們可以給我建議。 – zorox 2014-11-24 13:02:56

+0

從你看到什麼或讀到什麼,你不知道什麼? – SMA 2014-11-24 13:05:15

回答

0

我給你從你的Java應用程序的2個例子,你張貼文件服務。

的Apache的HttpClient:

String url = "https://yoururl.com"; 

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 

// add header 
post.setHeader("User-Agent", USER_AGENT); 

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); 
urlParameters.add(new BasicNameValuePair("xml", xmlString)); 

post.setEntity(new UrlEncodedFormEntity(urlParameters)); 

HttpResponse response = client.execute(post); 
System.out.println("\nSending 'POST' request to URL : " + url); 
System.out.println("Post parameters : " + post.getEntity()); 
System.out.println("Response Code : " + 
          response.getStatusLine().getStatusCode()); 

BufferedReader rd = new BufferedReader(
       new InputStreamReader(response.getEntity().getContent())); 

StringBuffer result = new StringBuffer(); 
String line = ""; 
while ((line = rd.readLine()) != null) { 
    result.append(line); 
} 

System.out.println(result.toString()); 

下面是一個例子,如何用java.net.URLConnection中做到這一點:

String url = "http://example.com"; 
String charset = "UTF-8"; 
String param1 = URLEncoder.encode("param1", charset); 
String param2 = URLEncoder.encode("param2", charset); 
String query = String.format("param1=%s&param2=%s", param1, param2); 

URLConnection urlConnection = new URL(url).openConnection(); 
urlConnection.setUseCaches(false); 
urlConnection.setDoOutput(true); // Triggers POST. 
urlConnection.setRequestProperty("accept-charset", charset); 
urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 

OutputStreamWriter writer = null; 
try { 
    writer = new OutputStreamWriter(urlConnection.getOutputStream(), charset); 
    writer.write(query); // Write POST query string (if any needed). 
} finally { 
    if (writer != null) try { writer.close(); } catch (IOException logOrIgnore) {} 
} 

InputStream result = urlConnection.getInputStream(); 
// Now do your thing with the result. 

感謝

溼婆庫馬爾SS

+0

謝謝Shiva Kumar SS我正在研究你的例子 – zorox 2014-11-24 13:30:40