C#如何访问某个PHP文件?
自己写的老是提示出错,代码如下:
- C# code
static string RegularFlowURL = "localhost/test.php";public static void Check(){ try { System.Net.ServicePointManager.Expect100Continue = false; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; request = WebRequest.Create(RegularFlowURL) as HttpWebRequest; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = 0; response = request.GetResponse() as HttpWebResponse; instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); string content = sr.ReadToEnd(); } catch (Exception ee) { MessageBox.Show("访问出错,请稍后再试"); }}
哪儿出问题了呢?求高手指点。
[解决办法]
这样试试
- C# code
static string RegularFlowURL = "http://localhost/test.php";public static void Check(){ try { System.Net.ServicePointManager.Expect100Continue = false; Stream instream = null; StreamReader sr = null; HttpWebResponse response = null; HttpWebRequest request = null; Encoding encoding = Encoding.UTF8; request = WebRequest.Create(RegularFlowURL) as HttpWebRequest; response = request.GetResponse() as HttpWebResponse; instream = response.GetResponseStream(); sr = new StreamReader(instream, encoding); string content = sr.ReadToEnd(); } catch (Exception ee) { MessageBox.Show("访问出错,请稍后再试"); }}
[解决办法]
如果再有错把 catch 里 ee.Message 打印出来看看是什么内容
[解决办法]
你又不传参数,
- C# code
request.Method = "POST";request.ContentType = "application/x-www-form-urlencoded";request.ContentLength = 0;