我正在使用HttpWebRequest.GetRequestStream
连接到Dot Net的其他PHP链接,将一些数据从.Net发送到PHP。但是当控件进入“Stream newrStream = myrRequest.GetRequestStream();”行时,它的前两次工作正常。并且从第三次开始延迟了几秒钟。如何解决这个问题。
我的编码是:
ASCIIEncoding Encode = new ASCIIEncoding();
string postDat = "Name=xxx";
byte[] datas = Encode.GetBytes(postDat);
HttpWebRequest myrRequest = HttpWebRequest)WebRequest.Create("http://www.xxx.php");
myrRequest.Method = "POST";
myrRequest.ContentType = "application/x-www-form-urlencoded";
myrRequest.ContentLength = datas.Length;
Stream newrStream = myrRequest.GetRequestStream();
// Send the data.
newrStream.Write(datas, 0, datas.Length);
newrStream.Close();
答案 0 :(得分:0)
要记住的第一件事是检查发送的URI,参数和标题,具体来说:
! * ' ( ) ; : @ & = + $ , / ? # [ ]
请注意,如果您想要发送较长的数据,建议您在邮件正文中发送:
string uri = string.Format("https://my-server.com/api/document?queryParameter={1}", "value");
WebRequest req = WebRequest.Create(uri);
req.Method = "POST";
string data = "Some data"; //Body data
ServicePointManager.Expect100Continue = false;
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
}
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
Stream GETResponseStream = resp.GetResponseStream();
StreamReader sr = new StreamReader(GETResponseStream);
var response = sr.ReadToEnd(); //Response
resp.Close(); //Close response
sr.Close(); //Close StreamReader