问题1:
Example :
System.Net.HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("test.brokerforum.com");
wr.UserAgent ???
该对象的 UserAgent 应该赋给它什么值才正确啊!可以的话麻烦给个代码--------------------------
问题2:
4- Get the request stream from the HttpWebRequest
 
Example : 
Stream requestStream = wr.GetRequestStream();
string  PostString;
//encode ?? 未定义
byte[] bytes = encode.GetBytes(PostString);
requestStream.Write(bytes, 0, bytes.Length);
bytes.Clone();
bytes = null;
想麻烦请问各位大侠们 文中为定义的 encode 应该定义为什么类型 才对啊!--------------------------------
以上代码仅仅是个片段 用于C#通过XML对数据库进行更新 但由于 代码不全 缺少对一些变量的定义 
偶又对这方面不是很熟悉 麻烦给位大侠帮忙看看谢谢 !

解决方案 »

  1.   

    //encode ?? 未定义 
    byte[] bytes = encode.GetBytes(PostString); 
    Encoding.Default.GetBytes...与你字符串的编码有关
      

  2.   

    wr.UserAgent ??? 是客户端的浏览器的类型
    比如User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12)
    就是wince ie浏览器
    你createa的时候应该自己就能取得到,而且哪个属性是只读的,不能副职哦!问题2
    用default就可以
    或者你可以自己指定
    Encoding.GetEncoding("gb2312");
      

  3.   

    问题1:                wr.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";问题2:你的字符串是什么字符集就用什么字符集。
      

  4.   

    那个字符串的内容如下:
    string PostString= @"MIME-Version: 1.0
                        Content-type: Multipart/Related; boundary=""MTO-part-boundary""; type=""application/mto-agent"" =
                        Content-Description: Business document
                        --MTO-part-boundary 
                        Content-type: text/xml 
                        Content-Description: Header 
                        <?xml version = ""1.0""?> 
                        <!DOCTYPE MTOHeader SYSTEM ""MTOHeader.dtd""> 
                        <MTOHeader> 
                                <MTOCode>InventoryUpdate</MTOCode> 
                                <MTOVersion>1.0</MTOVersion> 
                                <MessageInstanceId>123</MessageInstanceId> 
                                <InReplyTo/>  
                                <ActionCode>Action</ActionCode>  
                                <DateTime>" + CurrTime.ToString("dd/MM/yyyy") + "</DateTime>" +
                        @"
                        </MTOHeader> 
     
                        --MTO-part-boundary 
                        Content-type: text/xml 
                        Content-Description: Content 
                        <?xml version = ""1.0""?> 
                        <!DOCTYPE CmInventoryUpdateRequest SYSTEM ""CmInventoryUpdateRequest.dtd""> 
                        <CmInventoryUpdateRequest qtyUpdType=""" + qtyUpdType + @""">" +
                            @"
                            <Items>
                                <Item operation=""ADD""> 
                                    <PartNumber>" + partNumber + "</PartNumber>" +
                                    @"
                                    <Manufacturer>" + productManufacturer + "</Manufacturer>" + 
                                    @"
                                    <Description>" + productDescription + "</Description>" + 
                                    @"
                                    <DateCode>" + productDateCode + "</DateCode>" +
                                    @"
                                    <MemberPrice>" + productPrice + "</MemberPrice>" +
                                    @"
                                    <Quantity>" + productQuantity + "</Quantity>" +
                                    @"
                                </Item> 
                             </Items>
                        </CmInventoryUpdateRequest> 
                        --MTO-part-boundary--;";
      

  5.   

    整个代码是这样的 
    DateTime CurrTime = DateTime.Now;
    string qtyUpdType = "UpdateQty";
    string partNumber = "TEST_123";
    string productQuantity = "500";
    string productDateCode = "2006+";
    string productManufacturer = "IBM";
    string productDescription = "Some description";
    string productPrice = "100.99";
     string PostString= @"MIME-Version: 1.0
                        Content-type: Multipart/Related; boundary=""MTO-part-boundary""; type=""application/mto-agent"" =
                        Content-Description: Business document
                        --MTO-part-boundary 
                        Content-type: text/xml 
                        Content-Description: Header 
                        <?xml version = ""1.0""?> 
                        <!DOCTYPE MTOHeader SYSTEM ""MTOHeader.dtd""> 
                        <MTOHeader> 
                                <MTOCode>InventoryUpdate</MTOCode> 
                                <MTOVersion>1.0</MTOVersion> 
                                <MessageInstanceId>123</MessageInstanceId> 
                                <InReplyTo/>  
                                <ActionCode>Action</ActionCode>  
                                <DateTime>" + CurrTime.ToString("dd/MM/yyyy") + "</DateTime>" +
                        @"
                        </MTOHeader> 
     
                        --MTO-part-boundary 
                        Content-type: text/xml 
                        Content-Description: Content 
                        <?xml version = ""1.0""?> 
                        <!DOCTYPE CmInventoryUpdateRequest SYSTEM ""CmInventoryUpdateRequest.dtd""> 
                        <CmInventoryUpdateRequest qtyUpdType=""" + qtyUpdType + @""">" +
                            @"
                            <Items>
                                <Item operation=""ADD""> 
                                    <PartNumber>" + partNumber + "</PartNumber>" +
                                    @"
                                    <Manufacturer>" + productManufacturer + "</Manufacturer>" + 
                                    @"
                                    <Description>" + productDescription + "</Description>" + 
                                    @"
                                    <DateCode>" + productDateCode + "</DateCode>" +
                                    @"
                                    <MemberPrice>" + productPrice + "</MemberPrice>" +
                                    @"
                                    <Quantity>" + productQuantity + "</Quantity>" +
                                    @"
                                </Item> 
                             </Items>
                        </CmInventoryUpdateRequest> 
                        --MTO-part-boundary--;";
     //2- Create a HTTP connection to the server test.brokerforum.com (testing environment) or xml.brokerforum.com (prod environment).
     
    //Example :
    System.Net.HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("http://xml.brokerforum.com");
     //3- Set headers.  Note that the username and password must be encrypted using 64 base encoding
    //Example :string UserName = "zhu_XML";
    string Password = "dev123";
     wr.KeepAlive = true;
    wr.Headers = new WebHeaderCollection();
    wr.Method = "POST";
    wr.ContentType = "application/mto-agent";
    //wr.UserAgent = UserAgent;
    wr.UserAgent = @"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
    wr.ContentLength = PostString.Length;
    string base64EncodedAuthorizationString = UserName + ":" + Password;
    byte[] binaryData = new Byte[base64EncodedAuthorizationString.Length];
    binaryData = Encoding.UTF8.GetBytes(base64EncodedAuthorizationString);
    base64EncodedAuthorizationString = Convert.ToBase64String(binaryData);
    base64EncodedAuthorizationString = "Basic " +
    base64EncodedAuthorizationString;
    wr.Headers.Add("Authorization", base64EncodedAuthorizationString);
     //4- Get the request stream from the HttpWebRequest
     
    //Example : 
    Stream requestStream = wr.GetRequestStream();
    //byte[] bs = Encoding.ASCII.GetBytes(PostString);//using (Stream requestStream = wr.GetRequestStream())
    //{
    //    requestStream.Write(bs, 0, bs.Length);
    //}
    //Encoder encode;//byte[] bytes = encode.GetBytes(PostString);
    byte[] bytes = Encoding.Default.GetBytes(PostString);
    requestStream.Write(bytes, 0, bytes.Length);
    bytes.Clone();
    bytes = null;
                
    //5- Get the response from the server
     
    //Example :
     
    HttpWebResponse response = null;
    response = (HttpWebResponse)wr.GetResponse(); // 出错的地方 提示 "远程服务器返回错误: (500) 内部服务器错误。" 
     
    Stream responseStream = response.GetResponseStream();
     
    wr = null;
     
    // Print the response for debugging purposes
    Console.WriteLine(responseStream.ToString());
      

  6.   

    response = (HttpWebResponse)wr.GetResponse(); // 出错的地方 提示 "远程服务器返回错误: (500) 内部服务器错误。" 说明网站有错误,可能是认证错误或你的 PostString 格式不符合网站的要求,或者是你的编码与网站的不一致。具体要看网站的调试记录。
      

  7.   

    问题1: 
    Example : 
    System.Net.HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("test.brokerforum.com"); 
    wr.UserAgent ??? 
    该对象的 UserAgent 应该赋给它什么值才正确啊!可以的话麻烦给个代码 
    ===================================// Create a new 'HttpWebRequest' object to the mentioned URL.
    HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
    myHttpWebRequest.UserAgent=".NET Framework Test Client";
    // Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
    HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
    // Display the contents of the page to the console.
    Stream streamResponse=myHttpWebResponse.GetResponseStream();
    StreamReader streamRead = new StreamReader( streamResponse );
    Char[] readBuff = new Char[256];
    int count = streamRead.Read( readBuff, 0, 256 );
    Console.WriteLine("\nThe contents of HTML Page are :\n");    
    while (count > 0) 
    {
        String outputData = new String(readBuff, 0, count);
        Console.Write(outputData);
        count = streamRead.Read(readBuff, 0, 256);
    }
    // Release the response object resources.
    streamRead.Close();
    streamResponse.Close();
    myHttpWebResponse.Close();
      

  8.   

    问题2: 
    4- Get the request stream from the HttpWebRequest 
      
    Example :  
    Stream requestStream = wr.GetRequestStream(); 
    string  PostString; 
    //encode ?? 未定义 
    byte[] bytes = encode.GetBytes(PostString); 
    requestStream.Write(bytes, 0, bytes.Length); 
    bytes.Clone(); 
    bytes = null; 
    想麻烦请问各位大侠们 文中为定义的 encode 应该定义为什么类型 才对啊!
    ==============================================================
    应该先取一个服务器的编码设置,再设置成服务器一样的。