如题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();
    }
}

解决方案 »

  1.   

    using System.Net;
    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();
    }
    }
      

  2.   

    StringBuffer 在c#里面是stringbuilder
    3楼转换完毕啊,楼主可以试一试啊