我用Webrequest类向服务器发送xml请求,服务器收到后会反馈回来一个xml文件(其实不是文件,而是一个字符串,只不过这个字符串里面的内容是xml.发送方式采用的是二进制)。现在,问题来了:
如果我发送的xml字符串小一点的话,是没有问题的,可以收到服务器的回复。
但是只要我的xml字符串稍微大一些,就会出现异常:The operation has timed out.根据我在google上的搜索,我设定了Webrequest的Timeout属性,但结果还是会出现异常。
程序如下:
public String POST_TO_DMC(String url, String ServerName, String Version, String UserName, String Psd, String AcknowledgementsTo, String Data)
{
String strRec = null;
String poststr = null;
poststr += "&Version=" + Version;
poststr += "&Username=" + UserName;
poststr += "&Password=" + Psd;
poststr += "&XML=" + Data;
Encoding UTF8 = Encoding.UTF8;
Byte[] bytePost = UTF8.GetBytes(poststr);
try
{
Stream datas, datar;
WebRequest objrequest = WebRequest.Create(url);
objrequest.Timeout = 30000;
objrequest.Credentials=CredentialCache.DefaultCredentials ;
objrequest.Method = "POST";
objrequest.ContentLength = bytePost.Length;
objrequest.ContentType = "application/x-www-url-encoded";
datas = objrequest.GetRequestStream();
datas.Write(bytePost, 0, bytePost.Length);
datas.WriteTimeout=30000;
datas.Close(); WebResponse objresponse = objrequest.GetResponse();//发生WebException异常
datar = objresponse.GetResponseStream();
datar.WriteTimeout=30000;
StreamReader r = new StreamReader(datar);
strRec = r.ReadToEnd(); datar.Close();
r.Close();
objresponse.Close(); }
catch (ArgumentNullException Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
catch (NullReferenceException Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
catch (WebException Ex)
{
MessageBox.Show("WebException :" + Ex.Message);//抛出Timed out异常
}
catch (Exception Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
return strRec;
}
调试时的连接字符串,即函数里的poststr="&Version=4.0&Username=yjiang&Password=dfhjd&XML=<!DOCTYPE DMConnect SYSTEM \"DMConnect.dtd\"><DMConnect><CreateMailing><AcknowledgementsTo><EmailAddress>[email protected]</EmailAddress><Option>9</Option></AcknowledgementsTo><MailingType>StandardMailing</MailingType><ClientName>APAC_Sales</ClientName><SiteName>Hans_Practise</SiteName><CampaignName>Ryan_QuickSend</CampaignName><MailingName>DeskTopMsgTest</MailingName><ToList><ListName>ListTest</ListName></ToList><Filter></Filter><Subject>This is a test!!</Subject><MessageContent><TextBody><BodyText><![CDATA[Hi everyone:Welocome to Epsilon!\r\nName\r\n]]></BodyText></TextBody><HtmlBody><BodyText><![CDATA[<html><head><title>This is a test!</title><br /</head><body>Welcome to Epsilon</body></html>]]></BodyText></HtmlBody></MessageContent><HtmlOpenRateFlag>1</HtmlOpenRateFlag><AutosenseFlag>1</AutosenseFlag><CookieOptionFlag>0</CookieOptionFlag></CreateMailing></DMConnect>";
如果我发送的xml字符串小一点的话,是没有问题的,可以收到服务器的回复。
但是只要我的xml字符串稍微大一些,就会出现异常:The operation has timed out.根据我在google上的搜索,我设定了Webrequest的Timeout属性,但结果还是会出现异常。
程序如下:
public String POST_TO_DMC(String url, String ServerName, String Version, String UserName, String Psd, String AcknowledgementsTo, String Data)
{
String strRec = null;
String poststr = null;
poststr += "&Version=" + Version;
poststr += "&Username=" + UserName;
poststr += "&Password=" + Psd;
poststr += "&XML=" + Data;
Encoding UTF8 = Encoding.UTF8;
Byte[] bytePost = UTF8.GetBytes(poststr);
try
{
Stream datas, datar;
WebRequest objrequest = WebRequest.Create(url);
objrequest.Timeout = 30000;
objrequest.Credentials=CredentialCache.DefaultCredentials ;
objrequest.Method = "POST";
objrequest.ContentLength = bytePost.Length;
objrequest.ContentType = "application/x-www-url-encoded";
datas = objrequest.GetRequestStream();
datas.Write(bytePost, 0, bytePost.Length);
datas.WriteTimeout=30000;
datas.Close(); WebResponse objresponse = objrequest.GetResponse();//发生WebException异常
datar = objresponse.GetResponseStream();
datar.WriteTimeout=30000;
StreamReader r = new StreamReader(datar);
strRec = r.ReadToEnd(); datar.Close();
r.Close();
objresponse.Close(); }
catch (ArgumentNullException Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
catch (NullReferenceException Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
catch (WebException Ex)
{
MessageBox.Show("WebException :" + Ex.Message);//抛出Timed out异常
}
catch (Exception Ex)
{
MessageBox.Show("Message : " + Ex.Message);
}
return strRec;
}
调试时的连接字符串,即函数里的poststr="&Version=4.0&Username=yjiang&Password=dfhjd&XML=<!DOCTYPE DMConnect SYSTEM \"DMConnect.dtd\"><DMConnect><CreateMailing><AcknowledgementsTo><EmailAddress>[email protected]</EmailAddress><Option>9</Option></AcknowledgementsTo><MailingType>StandardMailing</MailingType><ClientName>APAC_Sales</ClientName><SiteName>Hans_Practise</SiteName><CampaignName>Ryan_QuickSend</CampaignName><MailingName>DeskTopMsgTest</MailingName><ToList><ListName>ListTest</ListName></ToList><Filter></Filter><Subject>This is a test!!</Subject><MessageContent><TextBody><BodyText><![CDATA[Hi everyone:Welocome to Epsilon!\r\nName\r\n]]></BodyText></TextBody><HtmlBody><BodyText><![CDATA[<html><head><title>This is a test!</title><br /</head><body>Welcome to Epsilon</body></html>]]></BodyText></HtmlBody></MessageContent><HtmlOpenRateFlag>1</HtmlOpenRateFlag><AutosenseFlag>1</AutosenseFlag><CookieOptionFlag>0</CookieOptionFlag></CreateMailing></DMConnect>";
而且函数中poststr应该加上:poststr+="Servername="+Servername;这一句谢谢!!!
我怀疑是不是我写的这个函数有什么问题,比如我定义的utf-8,byte什么的有长度限制。
服务器那边是好的,而且这个字符串绝对没超过4m