通过HttpWebRequest post数据的问题
工作上需要通过http协议往服务器post一条json字符串,服务器会返回成功或者故障信息,网络上找了些HttpWebRequest例子,都遇到同一种现象,相同的代码,如果我电脑打开Fiddler 4软件,执行结果能够成功,但我关闭Fiddler 4情况下,返回提示信息是“发送数据不能为空”。
不知道问题的原因,请求帮助,谢谢
贴一段代码,c#
public static string HttpPost(string url, string param)
{
byte[] byteArray = Encoding.UTF8.GetBytes(param);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml;charset=UTF-8";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string phpend = php.ReadToEnd();
return phpend;
}
不知道问题的原因,请求帮助,谢谢
贴一段代码,c#
public static string HttpPost(string url, string param)
{
byte[] byteArray = Encoding.UTF8.GetBytes(param);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "text/xml;charset=UTF-8";
webRequest.ContentLength = byteArray.Length;
Stream newStream = webRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader php = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string phpend = php.ReadToEnd();
return phpend;
}
...全文
相关推荐
C#中HttpWebRequest的用法详解
主要介绍了C#中HttpWebRequest的用法,以实例的形式详细叙述了HttpWebRequest类中GET与POST的用法,非常具有参考借鉴价值,需要的朋友可以参考下
C#中HttpWebRequest介绍
C#中HttpWebRequest使用介绍 GET HTTP HTTPS 请求
使用HttpWebRequest发送自定义POST请求
使用 HttpWebRequest发送自定义POST请求
浅谈C#中HttpWebRequest与HttpWebResponse的使用方法
本篇文章主要介绍了浅谈C#中HttpWebRequest与HttpWebResponse的使用方法,具有一定的参考价值,有兴趣的可以了解一下。
HttpWebRequest类的使用[文].pdf
HttpWebRequest类的使用[文].pdf


