问题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对数据库进行更新 但由于 代码不全 缺少对一些变量的定义
偶又对这方面不是很熟悉 麻烦给位大侠帮忙看看谢谢 !
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对数据库进行更新 但由于 代码不全 缺少对一些变量的定义
偶又对这方面不是很熟悉 麻烦给位大侠帮忙看看谢谢 !
解决方案 »
- 必须声明变量 '@id'错误提示。 高手指点下我该怎么改
- asp.net中实现添加附件时出错?
- C#里有类似Java的MyClass.class的东西吗?
- 关于C#的窗体怎么插入mtv
- DATASET中分组求和,统计怎么做
- 文件上传问题!!
- 如何拿C#调用cygwin环境下的C程序
- c#写串口收发工具,收到数据要开线程做数据处理吗?
- 超急的情况,系统一直都没事,突然访问不了。本地和外网,System.StackOverflowException
- 基于VS2010编程“C#多线程程序” 制作安装与部署问题之多线程混乱
- 这样的文件名如何生成,数字部分如何处理?
- 下载文件模块 “将对象引用设置到对象的实例”错误~
byte[] bytes = encode.GetBytes(PostString);
Encoding.Default.GetBytes...与你字符串的编码有关
比如User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows CE; IEMobile 6.12)
就是wince ie浏览器
你createa的时候应该自己就能取得到,而且哪个属性是只读的,不能副职哦!问题2
用default就可以
或者你可以自己指定
Encoding.GetEncoding("gb2312");
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--;";
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());
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();
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 应该定义为什么类型 才对啊!
==============================================================
应该先取一个服务器的编码设置,再设置成服务器一样的。