c# winform post传参数时,返回结果为错,请各位大神帮帮忙!!!

hatchgavin 2014-04-25 11:45:37
public string GetPage(string strURL, string SendData)
{
string strResult = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST";
if (SendData.Trim() != "")
{
byte[] data = Encoding.Default.GetBytes(SendData.Trim());
using (Stream stream = myHttpWebRequest.GetRequestStream())
{
stream.Write(data, 0, data.Length);

}
}
System.IO.StreamReader sr = new System.IO.StreamReader(myHttpWebRequest.GetResponse().GetResponseStream(), Encoding.Default);
strResult = sr.ReadToEnd();

return strResult;
}
调用: GetPage(”http://localhost:2545/Default.aspx“, “userName=12”)
但是一直返回为错误结果,正确应该返回username的值. 当用 直接用”http://localhost:2545/Default.aspx?userName=12” 时返回结果是对的。
...全文
457 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zijiang001 2014-04-25
  • 打赏
  • 举报
回复
myHttpWebRequest.ContentLength = data.Length; 加上看看
hatchgavin 2014-04-25
  • 打赏
  • 举报
回复

抓包获取的数据,post发的数据 "userName=12"也有呀!
bdmh 2014-04-25
  • 打赏
  • 举报
回复
估计是你传的参数不正确或者不完全,你可以用抓包工具抓一下(火狐浏览器的firedebug插件也可以),你正常访问时的post的信息,对着把它写全
hatchgavin 2014-04-25
  • 打赏
  • 举报
回复

知道问题在哪了。我这用的是 Request.QueryString 接收的参数 属于GET提交。而Request["userName"]是POST与GET提交都行的。
hatchgavin 2014-04-25
  • 打赏
  • 举报
回复

当不成功时返回1,成功时返回 userName值。

引用 5 楼 diaodiaop 的回复:
WebClient就可以了 何必写这么多代码...


public static string PostData(string ashx, string data)
{
try
{
var client = new WebClient();
client.Encoding = Encoding.UTF8;
var url = dosoft.StaticVariable.PostUrl + ashx;
if (data!="")
{
url +="?"+data;
}
var result = client.UploadString(url,data);
return result;
}
catch (Exception ex)
{
return "";
}
}


调用

var res = PostData("GetCard.ashx", "id=5");


如果是用这种方法 我就直接用 http://localhost:2545/Default.aspx?userName=12”就成了。但我想通过POST方法传参数过去。



public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
string id = context.Request["id"];
context.Response.Write(id);
}

by_封爱 版主 2014-04-25
  • 打赏
  • 举报
回复
实际上

var client = new WebClient();
client.Encoding = Encoding.UTF8;
var result = client.UploadString("http://xxx.xxx.xx.com/xxx.ashx?id=3&o=a","");
就可以了
zijiang001 2014-04-25
  • 打赏
  • 举报
回复
引用 4 楼 hatchgavin 的回复:
myHttpWebRequest.ContentLength = data.Length 加了也不成。
返回什么错误
by_封爱 版主 2014-04-25
  • 打赏
  • 举报
回复
WebClient就可以了 何必写这么多代码...

 public static string PostData(string ashx, string data)
        {
            try
            {
                var client = new WebClient();
                client.Encoding = Encoding.UTF8;
                var url = dosoft.StaticVariable.PostUrl + ashx;
                if (data!="")
                {
                    url +="?"+data;
                }
                var result = client.UploadString(url,data);
                return result;
            }
            catch (Exception ex)
            {
                return "";
            }
        }
调用

var res = PostData("GetCard.ashx", "id=5");

public void ProcessRequest (HttpContext context) 
{
        context.Response.ContentType = "text/plain";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        string id = context.Request["id"];
        context.Response.Write(id);
}

hatchgavin 2014-04-25
  • 打赏
  • 举报
回复
myHttpWebRequest.ContentLength = data.Length 加了也不成。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧