C#.NET HttpWebRequest错误:操作已超时 [英] C# .NET HttpWebRequest Error : operation has timed out
本文介绍了C#.NET HttpWebRequest错误:操作已超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好
我正在开发一个与PHP脚本通信的C#应用.
I am working on a C# app that communicates with a PHP script.
我使用以下代码将字符串数据(可能少于100kb)发布到PHP脚本中:
I use the following code to post string data (probably less than 100kb) to the PHP Script:
public string Post(string api_method, string api_data = "")
{
// Init
string Result = "";
string PostData = "";
HttpWebRequest MyHttpWebRequest;
Stream MyStream = null;
HttpWebResponse MyResponse = null;
StreamReader MyStreamReader = null;
// Post Data To infiniteSAGEBroker Script
try
{
// Load Cryptography Object
using (Rijndael MyRijndael = new Rijndael(Key, IV))
{
// Create Http Web Request
MyHttpWebRequest = (HttpWebRequest)WebRequest.Create(Settings.Default.SAGE_Broker_Url);
MyHttpWebRequest.Method = "POST";
MyHttpWebRequest.Timeout = (Settings.Default.HTTP_Web_Client_Timeout_Minutes * 60 * 1000);
MyHttpWebRequest.ReadWriteTimeout = (Settings.Default.HTTP_Web_Client_Timeout_Minutes * 60 * 1000);
MyHttpWebRequest.AllowWriteStreamBuffering = true;
MyHttpWebRequest.KeepAlive = false;
MyHttpWebRequest.UserAgent = "infiniteSAGEConnect v1.0";
MyHttpWebRequest.Credentials = cc;
// Build Post Data
string _api_method = MyRijndael.Encrypt(api_method);
string _api_data = MyRijndael.Encrypt(api_data);
PostData = "api_method=" + System.Web.HttpUtility.UrlEncode(_api_method) + "&api_data=" + System.Web.HttpUtility.UrlEncode(_api_data);
byte[] PostDataBytes = Encoding.UTF8.GetBytes(PostData);
// Post Data
MyHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
MyHttpWebRequest.ContentLength = PostDataBytes.Length;
MyStream = MyHttpWebRequest.GetRequestStream();
MyStream.Write(PostDataBytes, 0, PostDataBytes.Length);
// Get Response
MyResponse = (HttpWebResponse)MyHttpWebRequest.GetResponse();
MyStreamReader = new StreamReader(MyResponse.GetResponseStream());
string tmpResult = MyStreamReader.ReadToEnd();
// Decrypt Response
Result = MyRijndael.Decrypt(tmpResult);
}
}
catch (Exception e)
{
// Log Error
Result = "Error : " + e.Message + " | Operation : " + api_method + " | Data : " + PostData;
}
finally
{
// Release Resources
if (null != MyStreamReader) { MyStreamReader.Close(); }
if (null != MyResponse) { MyResponse.Close(); }
if (null != MyStream) { MyStream.Close(); }
}
// Finished
return Result;
}
推荐答案
这些可能不是您的代码问题.您的php和.NET主机都位于localhost?
Ths might not be your code problem.. are both of your php and .NET host in localhost?
如果没有,请执行ping -t并查看响应时间是什么.
If not then do a ping -t and see what is the response time.
还使用tracert查看是否可以访问服务器.
Use also tracert to see whether you can reach the server.
chanmm
这篇关于C#.NET HttpWebRequest错误:操作已超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文