首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 请教2个小问题 [已结贴,结贴人:scmail81]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 11:01:59 楼主
    问题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对数据库进行更新 但由于 代码不全 缺少对一些变量的定义
    偶又对这方面不是很熟悉 麻烦给位大侠帮忙看看谢谢 !


    100  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Tll_W
    • 等级:
    发表于:2008-05-26 11:05:131楼 得分:0
    //encode ?? 未定义
    byte[] bytes = encode.GetBytes(PostString);
    Encoding.Default.GetBytes...与你字符串的编码有关
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 11:17:382楼 得分:30
    wr.UserAgent ??? 是客户端的浏览器的类型
    比如User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12)
    就是wince ie浏览器
    你createa的时候应该自己就能取得到,而且哪个属性是只读的,不能副职哦!

    问题2
    用default就可以
    或者你可以自己指定
    Encoding.GetEncoding("gb2312");


    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 11:21:223楼 得分:0
    问题1:               

    C# code
    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:

    你的字符串是什么字符集就用什么字符集。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 11:23:564楼 得分:0
    学习
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 11:27:065楼 得分:0
    那个字符串的内容如下:
    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--;";
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Tll_W
    • 等级:
    发表于:2008-05-26 11:44:596楼 得分:0
    引用 5 楼 scmail81 的回复:
    那个字符串的内容如下:
    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 
                …

    用default应该行的
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 13:30:157楼 得分:0
    谢谢各位的帮忙
    我已经按照上面的更改了代码
    现在可以编译通过了
    但运行是 提示 "远程服务器返回错误: (500) 内部服务器错误。"
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 13:31:478楼 得分:0
    整个代码是这样的

    C# code
    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());
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 13:32:409楼 得分:0
    各位大侠 麻烦给支个招哈!谢谢了!
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 15:03:3010楼 得分:0
    2楼 正确
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-26 15:16:1311楼 得分:0
    up
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Dergen
    • 等级:
    发表于:2008-05-26 15:31:3312楼 得分:20
    response = (HttpWebResponse)wr.GetResponse(); // 出错的地方 提示 "远程服务器返回错误: (500) 内部服务器错误。"

    说明网站有错误,可能是认证错误或你的 PostString 格式不符合网站的要求,或者是你的编码与网站的不一致。具体要看网站的调试记录。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • beblue
    • 等级:
    发表于:2008-05-26 15:32:2813楼 得分:0
    引用 6 楼 Tll_W 的回复:
    引用 5 楼 scmail81 的回复:
    那个字符串的内容如下: 
    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-D…
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • Dergen
    • 等级:
    发表于:2008-05-27 08:42:2814楼 得分:0
    还是在服务器端检查一下是什么错误
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-27 08:57:1015楼 得分:30
    问题1:
    Example :
    System.Net.HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create("test.brokerforum.com");
    wr.UserAgent ???
    该对象的 UserAgent 应该赋给它什么值才正确啊!可以的话麻烦给个代码
    ===================================
    C# code
    // 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();
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    发表于:2008-05-27 08:58:4316楼 得分:20
    问题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 应该定义为什么类型 才对啊!
    ==============================================================
    应该先取一个服务器的编码设置,再设置成服务器一样的。
    修改 删除 举报 引用 回复

    网站简介广告服务网站地图帮助联系方式诚聘英才English 问题报告
    北京创新乐知广告有限公司 版权所有 京 ICP 证 070598 号
    世纪乐知(北京)网络技术有限公司 提供技术支持
    Copyright © 2000-2008, CSDN.NET, All Rights Reserved