读书人

关于用Post发送数据的有关问题

发布时间: 2012-12-25 16:18:28 作者: rapoo

关于用Post发送数据的问题,急.........
本帖最后由 wuxi_jj 于 2012-12-15 09:12:22 编辑 程序一:
public static string RequestUrl(string a)
{
try
{
string sessionkey = GetSession();
string url = "http://192.168.0.11/WebService/Service.asmx/ImportStyle";
a = a.Replace("\r\n",string.Empty); string sendData = "sessionkey="+sessionkey +a ;
string outputString = String.Empty;
HttpWebRequest hwrq = (HttpWebRequest)HttpWebRequest.Create(url);
hwrq.ContentType = "application/x-www-form-urlencoded";
hwrq.Method = "POST"; byte[] postData = System.Text.Encoding.UTF8.GetBytes(sendData);
hwrq.ContentLength = postData.Length;
Stream writeStream = hwrq.GetRequestStream();
writeStream.Write(postData, 0, postData.Length);
writeStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)hwrq.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
outputString = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
throw ex;
}
return null;
}
程序二:
报错:{"远程服务器返回错误: (500) 内部服务器错误。"}
public static string RequestUrl(string a)
{
try
{
string sessionkey = GetSession();
string url = "http://192.168.0.11/WebService/Service.asmx/ImportStyle?sessionkey="+sessionkey ; a = a.Replace("\r\n",string.Empty);

string sendData = a ; string outputString = String.Empty;
HttpWebRequest hwrq = (HttpWebRequest)HttpWebRequest.Create(url);
hwrq.ContentType = "application/x-www-form-urlencoded";
hwrq.Method = "POST"; byte[] postData = System.Text.Encoding.UTF8.GetBytes(sendData);
hwrq.ContentLength = postData.Length;
Stream writeStream = hwrq.GetRequestStream();
writeStream.Write(postData, 0, postData.Length);
writeStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)hwrq.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
outputString = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
throw ex;
}
return null;
}
请问用POST方式能在URL后面带?加参数吗,接收端怎么接收
[解决办法]
参考 http://www.codeproject.com/Articles/2487/Discover-WEB-HTTP-GET-POST-Utility
[解决办法]
你的方法写的不对
Post不是这样提交的,你看我的文章写,写的很详细
C# HttpWebRequest 绝技
GET
[解决办法]
POST方法的使用都有详细说明

[解决办法]

引用:
请问用POST方式能在URL后面带?加参数吗,接收端怎么接收


当然。不然互联网还怎么混?!

接收端要看你用什么做开发。例如asp.net,那么自然就是 Request.Query[...]啦。

但是由于(在中文操作系统上运行的)浏览器的问题,你最好对于url参数(也就是这个sessionkey变量的值)进行url编码。在url路径上的特殊内容大多不需要编码,但是在url参数上的特殊内容一定要编码。

读书人网 >C#

热点推荐