如题public class XMLSocketSearch { public static String postMessage = ""; public XMLSocketSearch() throws Exception {
try {
// Search for part number 123
XMLSocketSearch( "123" );
} catch ( Exception e ) {
System.out.println( "\nAn error occured trying to execute search.\n" );
e.printStackTrace();
}
} public void XMLSocketSearch( String pPartNo ) throws Exception { // Setup the Raw Socket connection.
Socket wSocket = new Socket( "test.brokerforum.com", 80 ); // Get the outputstream so we can write to it.
OutputStream wOut = wSocket.getOutputStream(); String wUser = new String( "usernameHere" + ":" + "passwordHere" ); String wEncodedPassword = URLEncoder.encode( wUser );
String wEncoding = new sun.misc.BASE64Encoder().encode( wUser.getBytes() ); postMessage =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE ProductAvailabilityQuery SYSTEM " + "\"MediagrifProductAvailabilityQueryMessageGuideline_v1_0.dtd\">\n" +
"<ProductAvailabilityQuery>\n" +
"<ID>\n" +
"<TransactionName>MbcProductAvailabilityQuery</TransactionName>\n" +
"<TransactionVersion>1.0</TransactionVersion>\n" +
"</ID>\n" +
" <QueryConstraint>\n" +
" <VerifiedOfferOnly>false</VerifiedOfferOnly>\n" +
" <MaxNoOfResults>25</MaxNoOfResults>\n" +
" <ExactMatch>false</ExactMatch>\n" +
" <LineNo>0</LineNo>\n" +
" </QueryConstraint>\n" +
" <ProductAvailability>\n" +
" <ProductId>123</ProductId>\n" +
" <ProductQty>0</ProductQty>\n" +
" <ProductDC></ProductDC>\n" +
" <ProductMfg></ProductMfg>\n" +
" </ProductAvailability>\n" +
"</ProductAvailabilityQuery>\n"; // Posting message to send to the server.
String wPostHeader=
"POST /bfxml HTTP/1.1\r\n" +
"Authorization: Basic " + wEncoding + "\r\n" +
"Connection: close\r\n" +
"Content-Type: text/xml\r\n" +
"User-Agent: Java1.2.2\r\n" +
"Host: test.brokerforum.com\r\n" +
"Accept: text/xml\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Content-length: " + postMessage.length() + "\r\n\r\n" + postMessage; // OutputStream only accepts byte array.
wOut.write( wPostHeader.getBytes() ); // If wOut has any left over data, flush it.
wOut.flush(); StringBuffer wResult = new StringBuffer(); // Setup an inputstreamreader to handle incoming data.
// Get the response back from the XML Search server . . .
InputStreamReader wReader = new InputStreamReader(wSocket.getInputStream() );
BufferedReader wBuff = new BufferedReader( wReader );
String wUrlContent = null; // . . . then read it one line after the other
while ( ( wUrlContent = wBuff.readLine() ) != null ) {
wResult.append( wUrlContent + "\r\n" );
}
wBuff.close(); // Close the Socket
wSocket.close(); // Display the result on screen.
System.out.println( wResult );
} public static void main(String[] args) throws Exception{
XMLSocketSearch wSearch = new XMLSocketSearch();
}
}
try {
// Search for part number 123
XMLSocketSearch( "123" );
} catch ( Exception e ) {
System.out.println( "\nAn error occured trying to execute search.\n" );
e.printStackTrace();
}
} public void XMLSocketSearch( String pPartNo ) throws Exception { // Setup the Raw Socket connection.
Socket wSocket = new Socket( "test.brokerforum.com", 80 ); // Get the outputstream so we can write to it.
OutputStream wOut = wSocket.getOutputStream(); String wUser = new String( "usernameHere" + ":" + "passwordHere" ); String wEncodedPassword = URLEncoder.encode( wUser );
String wEncoding = new sun.misc.BASE64Encoder().encode( wUser.getBytes() ); postMessage =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE ProductAvailabilityQuery SYSTEM " + "\"MediagrifProductAvailabilityQueryMessageGuideline_v1_0.dtd\">\n" +
"<ProductAvailabilityQuery>\n" +
"<ID>\n" +
"<TransactionName>MbcProductAvailabilityQuery</TransactionName>\n" +
"<TransactionVersion>1.0</TransactionVersion>\n" +
"</ID>\n" +
" <QueryConstraint>\n" +
" <VerifiedOfferOnly>false</VerifiedOfferOnly>\n" +
" <MaxNoOfResults>25</MaxNoOfResults>\n" +
" <ExactMatch>false</ExactMatch>\n" +
" <LineNo>0</LineNo>\n" +
" </QueryConstraint>\n" +
" <ProductAvailability>\n" +
" <ProductId>123</ProductId>\n" +
" <ProductQty>0</ProductQty>\n" +
" <ProductDC></ProductDC>\n" +
" <ProductMfg></ProductMfg>\n" +
" </ProductAvailability>\n" +
"</ProductAvailabilityQuery>\n"; // Posting message to send to the server.
String wPostHeader=
"POST /bfxml HTTP/1.1\r\n" +
"Authorization: Basic " + wEncoding + "\r\n" +
"Connection: close\r\n" +
"Content-Type: text/xml\r\n" +
"User-Agent: Java1.2.2\r\n" +
"Host: test.brokerforum.com\r\n" +
"Accept: text/xml\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Content-length: " + postMessage.length() + "\r\n\r\n" + postMessage; // OutputStream only accepts byte array.
wOut.write( wPostHeader.getBytes() ); // If wOut has any left over data, flush it.
wOut.flush(); StringBuffer wResult = new StringBuffer(); // Setup an inputstreamreader to handle incoming data.
// Get the response back from the XML Search server . . .
InputStreamReader wReader = new InputStreamReader(wSocket.getInputStream() );
BufferedReader wBuff = new BufferedReader( wReader );
String wUrlContent = null; // . . . then read it one line after the other
while ( ( wUrlContent = wBuff.readLine() ) != null ) {
wResult.append( wUrlContent + "\r\n" );
}
wBuff.close(); // Close the Socket
wSocket.close(); // Display the result on screen.
System.out.println( wResult );
} public static void main(String[] args) throws Exception{
XMLSocketSearch wSearch = new XMLSocketSearch();
}
}
解决方案 »
- 一个小问题,静态函数里定义的变量一定是静态变量吗
- 请问C#怎么改变默认的重载方法
- 各位大虾,弟子出学c#,想下一个visual C# express,要中文版的,并且不要visual studio太大了,装不下。能否告诉我地址?我下了好多还是
- c# 如果一个文件已经存在,怎样重写它?
- ComboBox索引下拉条自动展开问题
- 批量下载一批网页。。如果使用多线程进行下载。。由于每个网页的下载完成时间不同。。如果判断所有网页下载完成。。
- 为什么说DataTable.Dispose()是释放非托管资源?
- DataTime对象比较大小
- 关于登录判断的一个小问题、
- 请各路高手进来看看,一个基本语法的问题,但是比较难.
- 关于标题栏的制作
- C#如何动态绑定数据库
using System.Net.Sockets;
using System.IO;
using System.Web;public class XMLSocketSearch
{
public static String postMessage = ""; public XMLSocketSearch()
{
try {
// Search for part number 123
Search("123");
} catch (Exception e) {
System.Console.WriteLine( "\nAn error occured trying to execute search.\n" + e.Message);
}
} public XMLSocketSearch(String pPartNo)
{
Search(pPartNo);
} private void Search(String pPartNo)
{ // Setup the Raw Socket connection.
IPHostEntry ipHost = Dns.GetHostEntry("test.brokerforum.com");
IPEndPoint address = new IPEndPoint(ipHost.AddressList[0], 80); Socket wSocket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); wSocket.Connect(address); // Get the outputstream so we can write to it.
Stream wOut = new NetworkStream(wSocket); String wUser = "usernameHere" + ":" + "passwordHere"; String wEncodedPassword = HttpUtility.UrlEncode(wUser);
String wEncoding = Convert.ToBase64String(Encoding.Default.GetBytes(wUser)); postMessage =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<!DOCTYPE ProductAvailabilityQuery SYSTEM " + "\"MediagrifProductAvailabilityQueryMessageGuideline_v1_0.dtd\">\n" +
"<ProductAvailabilityQuery>\n" +
"<ID>\n" +
"<TransactionName>MbcProductAvailabilityQuery</TransactionName>\n" +
"<TransactionVersion>1.0</TransactionVersion>\n" +
"</ID>\n" +
" <QueryConstraint>\n" +
" <VerifiedOfferOnly>false</VerifiedOfferOnly>\n" +
" <MaxNoOfResults>25</MaxNoOfResults>\n" +
" <ExactMatch>false</ExactMatch>\n" +
" <LineNo>0</LineNo>\n" +
" </QueryConstraint>\n" +
" <ProductAvailability>\n" +
" <ProductId>123</ProductId>\n" +
" <ProductQty>0</ProductQty>\n" +
" <ProductDC></ProductDC>\n" +
" <ProductMfg></ProductMfg>\n" +
" </ProductAvailability>\n" +
"</ProductAvailabilityQuery>\n"; // Posting message to send to the server.
String wPostHeader=
"POST /bfxml HTTP/1.1\r\n" +
"Authorization: Basic " + wEncoding + "\r\n" +
"Connection: close\r\n" +
"Content-Type: text/xml\r\n" +
"User-Agent: Java1.2.2\r\n" +
"Host: test.brokerforum.com\r\n" +
"Accept: text/xml\r\n" +
"Accept-Encoding: gzip, deflate\r\n" +
"Content-length: " + postMessage.Length + "\r\n\r\n" + postMessage; byte[] data = Encoding.Default.GetBytes(wPostHeader); // OutputStream only accepts byte array.
wOut.Write(data, 0, data.Length); // If wOut has any left over data, flush it.
wOut.Flush(); StringBuilder wResult = new StringBuilder(); // Setup an inputstreamreader to handle incoming data.
// Get the response back from the XML Search server . . .
StreamReader wReader = new StreamReader(wOut, Encoding.Default, true);
String wUrlContent = null; // . . . then read it one line after the other
while ( ( wUrlContent = wReader.ReadLine() ) != null ) {
wResult.Append( wUrlContent + "\r\n" );
} // Close the Socket
wSocket.Shutdown(SocketShutdown.Both);
wSocket.Close();
// Display the result on screen.
System.Console.WriteLine(wResult.ToString());
}
public static void main(String[] args)
{
XMLSocketSearch wSearch = new XMLSocketSearch();
}
}
3楼转换完毕啊,楼主可以试一试啊