⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codeprojectclient.java

📁 You may have heard the Latest Brief web service on CodeProject.com. It provides several methods for
💻 JAVA
字号:

import java.io.*;
import java.net.*;

public class CodeProjectClient
{
	public static void main(String[] args) throws Exception 
	{
		// get lastest articles
		System.out.println("\nReturn XML for the latest code project articles:\n");
		System.out.print(GetLatestArticleBrief(20));
		System.out.println("");	
		
		// get latest lounge posts
		System.out.println("\nReturn XML for the latest code project lounge posts:\n");
		System.out.print(GetLatestLoungeBrief(10));
		System.out.println("");	

		// get latest article comments
		System.out.println("\nReturn XML for the latest code project article comments:\n");
		System.out.print(GetLatestCommentsBrief(10));
		System.out.println("");

		// get newsflash	
		System.out.println("\nReturn XML for the code project newsflash:\n");
		System.out.print(GetNewsflash());
		System.out.println("");	
	}

	public static String GetLatestArticleBrief(int nNumberOfArticles)
	{
		try
		{
			// compose the soap message
			StringBuffer msgBuffer = new StringBuffer(64*1024);
			msgBuffer.append("<SOAP-ENV:Envelope xmlns:SOAPSDK1=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAPSDK2=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAPSDK3=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetLatestArticleBrief xmlns=\"http://codeproject.com/webservices/\"><NumArticles xmlns:SOAPSDK4=\"http://codeproject.com/webservices/\">");
			msgBuffer.append(nNumberOfArticles);
			msgBuffer.append("</NumArticles></GetLatestArticleBrief></SOAP-ENV:Body></SOAP-ENV:Envelope>");

			// send the soap message
			byte[] pRequest = msgBuffer.toString().getBytes();
			HttpURLConnection httpConn = GetHttpConnection("http://www.codeproject.com/webservices/latest.asmx?wsdl", "http://codeproject.com/webservices/GetLatestArticleBrief");
			httpConn.setRequestProperty("Content-Length", String.valueOf( pRequest.length ) );
			OutputStream out = httpConn.getOutputStream();
			out.write(pRequest);    
			out.close();

			// return the output
			return GetResponseData(httpConn);			
		}
		catch(Exception oBug)
		{
			System.out.println("Exception in CodeProjectService::GetLatestArticleBrief\r\n\t"+oBug.getMessage());
			return "";
		}
	}

	public static String GetLatestLoungeBrief(int nNumberOfPosts)
	{
		try
		{
			// compose the soap message
			StringBuffer msgBuffer = new StringBuffer(64*1024);
			msgBuffer.append("<SOAP-ENV:Envelope xmlns:SOAPSDK1=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAPSDK2=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAPSDK3=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetLatestLoungeBrief xmlns=\"http://codeproject.com/webservices/\"><NumMsgs xmlns:SOAPSDK4=\"http://codeproject.com/webservices/\">");
			msgBuffer.append(nNumberOfPosts);
			msgBuffer.append("</NumMsgs></GetLatestLoungeBrief></SOAP-ENV:Body></SOAP-ENV:Envelope>");

			// send the soap message
			byte[] pRequest = msgBuffer.toString().getBytes();
			HttpURLConnection httpConn = GetHttpConnection("http://www.codeproject.com/webservices/latest.asmx?wsdl", "http://codeproject.com/webservices/GetLatestLoungeBrief");
			httpConn.setRequestProperty("Content-Length", String.valueOf( pRequest.length ) );
			OutputStream out = httpConn.getOutputStream();
			out.write(pRequest);    
			out.close();

			// return the output
			return GetResponseData(httpConn);			
		}
		catch(Exception oBug)
		{
			System.out.println("Exception in CodeProjectService::GetLatestLoungeBrief\r\n\t"+oBug.getMessage());
			return "";
		}
	}

	public static String GetLatestCommentsBrief(int nNumberOfMsgs)
	{
		try
		{
			// compose the soap message
			StringBuffer msgBuffer = new StringBuffer(64*1024);
			msgBuffer.append("<SOAP-ENV:Envelope xmlns:SOAPSDK1=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAPSDK2=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAPSDK3=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetLatestCommentsBrief xmlns=\"http://codeproject.com/webservices/\"><NumMsgs xmlns:SOAPSDK4=\"http://codeproject.com/webservices/\">");
			msgBuffer.append(nNumberOfMsgs);
			msgBuffer.append("</NumMsgs></GetLatestCommentsBrief></SOAP-ENV:Body></SOAP-ENV:Envelope>");

			// send the soap message
			byte[] pRequest = msgBuffer.toString().getBytes();
			HttpURLConnection httpConn = GetHttpConnection("http://www.codeproject.com/webservices/latest.asmx?wsdl", "http://codeproject.com/webservices/GetLatestCommentsBrief");
			httpConn.setRequestProperty("Content-Length", String.valueOf( pRequest.length ) );
			OutputStream out = httpConn.getOutputStream();
			out.write(pRequest);    
			out.close();

			// return the output
			return GetResponseData(httpConn);			
		}
		catch(Exception oBug)
		{
			System.out.println("Exception in CodeProjectService::GetLatestCommentsBrief\r\n\t"+oBug.getMessage());
			return "";
		}
	}

	public static String GetNewsflash()
	{
		try
		{
			// compose the soap message
			StringBuffer msgBuffer = new StringBuffer(64*1024);
			msgBuffer.append("<SOAP-ENV:Envelope xmlns:SOAPSDK1=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAPSDK2=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAPSDK3=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Body><GetNewsflash xmlns=\"http://codeproject.com/webservices/\"/></SOAP-ENV:Body></SOAP-ENV:Envelope>");
			
			// send the soap message
			byte[] pRequest = msgBuffer.toString().getBytes();
			HttpURLConnection httpConn = GetHttpConnection("http://www.codeproject.com/webservices/latest.asmx?wsdl", "http://codeproject.com/webservices/GetNewsflash");
			httpConn.setRequestProperty("Content-Length", String.valueOf( pRequest.length ) );
			OutputStream out = httpConn.getOutputStream();
			out.write(pRequest);    
			out.close();

			// return the output
			return GetResponseData(httpConn);			
		}
		catch(Exception oBug)
		{
			System.out.println("Exception in CodeProjectService::GetNewsflash\r\n\t"+oBug.getMessage());
			return "";
		}
	}

	static HttpURLConnection GetHttpConnection(String sServiceURL, String sSoapAction) throws Exception
	{
		// create the http connection
		URL url = new URL(sServiceURL);
		URLConnection conn = url.openConnection();
		HttpURLConnection httpConn = (HttpURLConnection)conn;
      		
		// set properties of the connection
		httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
		httpConn.setRequestProperty("SOAPAction", sSoapAction);
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		httpConn.setUseCaches(false);

		// return the connection
		return httpConn;
	}

	static String GetResponseData(HttpURLConnection httpConn) throws Exception
	{
		InputStream streamIn = httpConn.getInputStream();
		int nTotal = 0;
		int nSize = 64*1024;
		byte pResult[] = new byte[nSize]; 
		int nRead = 0;
		while(true) 
		{ 
			Thread.currentThread().sleep(1);
			nRead = streamIn.read(pResult, nTotal, nSize-nTotal);
			if(nRead<0) break;
			nTotal += nRead;
			if(nTotal==nSize)
			{
				nSize *= 2;
				byte pTemp[] = new byte[nSize];
				System.arraycopy(pResult, 0, pTemp, 0, nTotal);
				pResult = pTemp;
			}
		}
		byte[] pOutput = new byte[nTotal];
		return new String(pResult, 0, nTotal);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -