这是java代码:
但是,c#中应该怎么做呢?拜托哪位大神帮帮忙
你把你查的资料的代码贴出来,然后指出你为什么有“我有其他的参数呢!也要post,那怎么办呢”这样的疑问。
哥,其实是这样的,这是我写的代码:
if(requestMessage.Content.Contains("我要上网")) { if (UserProfile.UserCode == "6d4603790dfc47bbb4cacde8f24e6fb7") { string res = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[{3}]]></MsgType> <Content><![CDATA[{4}]]></Content> </xml> ", requestMessage.FromUserName, requestMessage.ToUserName, DateTime.Now, requestMessage.MsgType, requestMessage.Content); string ss=callProxy("", res); var responseMessage = CreateResponseMessage<ResponseMessageText>(); responseMessage.Content = ss; return responseMessage; } }
我要向一个服务器url post 类型为xml的数据,所以,我就拼接了res,但是,我还有Signature,Timestamp,Nonce 这几个参数也要传过去,所以,我不知道怎么写代码
我的post代码如下:
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postStr) { Sign = System.Web.HttpUtility.UrlEncode(Sign, System.Text.Encoding.UTF8); Timesp = System.Web.HttpUtility.UrlEncode(Timesp, System.Text.Encoding.UTF8); Non = System.Web.HttpUtility.UrlEncode(Non, System.Text.Encoding.UTF8); postStr = System.Web.HttpUtility.UrlEncode(postStr, System.Text.Encoding.UTF8); string postData = string.Format("postDataStr={0}&Sign={1}&Timesp={2}&Non={3}", postStr, Sign, Timesp, Non); // 要发放的数据 byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //请求 request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount(postData); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(byteArray); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@魔女小溪: 请问你这几个参数“Signature,Timestamp,Nonce”是对应 url 参数,还是对应 Request Entity Body 呢?
@Launcher: 对应url的
@魔女小溪:
request.ContentLength = Encoding.UTF8.GetByteCount(postData);
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
这两行代码不匹配,要么都用 UTF8,要么都用 GB2312,
@Launcher: 恩恩,我就是对于你说的那个Request Entity Body 不懂,我百度看到的都是java的代码,里面就有request entity这东东,但就是没看到c#有哪个这么写了
@魔女小溪: 对应 url 的话,你应该这么 post,以 HttpWebRequest 为例。
request = (HttpWebRequest)WebRequest.Create(Url+"postDataStr={0}&Sign={1}&Timesp={2}&Non={3}");
string res = string.Format(@"<xml>
<ToUserName><![CDATA[{0}]]></ToUserName>
<FromUserName><![CDATA[{1}]]></FromUserName>
<CreateTime>{2}</CreateTime>
<MsgType><![CDATA[{3}]]></MsgType>
<Content><![CDATA[{4}]]></Content>
</xml> ",
request.ContentType = "application/xml";
request.ContentLength = Encoding.UTF8.GetByteCount(res);
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("UTF-8"));
myStreamWriter.Write(res);
@Launcher: 额,那xml这个地方myStreamWriter.Write(res);
就是对应的request entity吗?
@魔女小溪: 对。
@Launcher: 是这样的吗?postStr是字符串xml
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postStr) { Sign = System.Web.HttpUtility.UrlEncode(Sign, System.Text.Encoding.UTF8); Timesp = System.Web.HttpUtility.UrlEncode(Timesp, System.Text.Encoding.UTF8); Non = System.Web.HttpUtility.UrlEncode(Non, System.Text.Encoding.UTF8); //postStr = System.Web.HttpUtility.UrlEncode(postStr, System.Text.Encoding.UTF8); string postData = string.Format("?Sign={1}&Timesp={2}&Non={3}",Sign, Timesp, Non); // 其他参数 //byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //请求 request = (HttpWebRequest)WebRequest.Create(Url + postData); request.Method = "POST"; request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(postStr); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postStr); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@魔女小溪: 不对。你把电话告诉我,我一句一句告诉你得了。
@Launcher: (⊙o⊙)…,不能写代码吗?电话费太贵了,别浪费钱了,(*^__^*) 嘻嘻……
@魔女小溪: 大神都着急了。。。让大神给你远程不就好了
@羽商宫: (⊙o⊙)…
@魔女小溪:
@魔女小溪: 对应 url 的话,你应该这么 post,以 HttpWebRequest 为例。
request = (HttpWebRequest)WebRequest.Create(Url+"postDataStr={0}&Sign={1}&Timesp={2}&Non={3}");
string res = string.Format(@"<xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[{3}]]></MsgType> <Content><![CDATA[{4}]]></Content> </xml> ",
request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(res);
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("UTF-8"));
myStreamWriter.Write(res);
这是之前我给你回答的,现在你一条一条的对比你的代码,你看你哪里写的不对。如果你要说就是照着这么做的话,那么如果我举出一条你没照着这么做的代码,你能给我 100 块钱不?
@Launcher: postDataStr是xml字符串,你确定是这么写?
@Launcher: 还有,这个地方:(HttpWebRequest)WebRequest.Create(Url+"postDataStr={0}&Sign={1}&Timesp={2}&Non={3}");
这个没有赋值呀,只是给了占位符罢了
@Launcher: 这样嘛?
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postStr) { Sign = System.Web.HttpUtility.UrlEncode(Sign, System.Text.Encoding.UTF8); Timesp = System.Web.HttpUtility.UrlEncode(Timesp, System.Text.Encoding.UTF8); Non = System.Web.HttpUtility.UrlEncode(Non, System.Text.Encoding.UTF8); //postStr = System.Web.HttpUtility.UrlEncode(postStr, System.Text.Encoding.UTF8); //string postData = string.Format("?Sign={1}&Timesp={2}&Non={3}",Sign, Timesp, Non); // 其他参数 //byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //请求 request = (HttpWebRequest)WebRequest.Create(Url + "postDataStr={0}&Sign={1}&Timesp={2}&Non={3}"); request.Method = "POST"; request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(postStr); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postStr); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@Launcher:
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postStr) { Sign = System.Web.HttpUtility.UrlEncode(Sign, System.Text.Encoding.UTF8); Timesp = System.Web.HttpUtility.UrlEncode(Timesp, System.Text.Encoding.UTF8); Non = System.Web.HttpUtility.UrlEncode(Non, System.Text.Encoding.UTF8); //postStr = System.Web.HttpUtility.UrlEncode(postStr, System.Text.Encoding.UTF8); string postData = string.Format("?Sign={0}&Timesp={1}&Non={2}",Sign, Timesp, Non); // 其他参数 //byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //请求 request = (HttpWebRequest)WebRequest.Create(Url +postData); request.Method = "POST"; request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(postStr); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postStr); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@魔女小溪: 不需要 UrlEncode,你最后 POST 请求的地址应该是这样:
WebRequest.Create("http://sssss.ss.ss/moxq.aspx?Sign=ssouwer&Timesp=sssss&Non=23434");
@Launcher: 就是每个参数,例如sign等,不需要经过urlencode是吧
@Launcher: 那就是这样的吧
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postStr) { //Sign = System.Web.HttpUtility.UrlEncode(Sign, System.Text.Encoding.UTF8); //Timesp = System.Web.HttpUtility.UrlEncode(Timesp, System.Text.Encoding.UTF8); //Non = System.Web.HttpUtility.UrlEncode(Non, System.Text.Encoding.UTF8); //postStr = System.Web.HttpUtility.UrlEncode(postStr, System.Text.Encoding.UTF8); string postData = string.Format("?Sign={0}&Timesp={1}&Non={2}",Sign, Timesp, Non); // 其他参数 //byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); //请求 request = (HttpWebRequest)WebRequest.Create(Url +postData); request.Method = "POST"; request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(postStr); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postStr); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@Launcher: 字符串xml数据需要转换成这样嘛?
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postStr);
@魔女小溪: StreamWriter.Write(string) 会帮你做 System.Text.Encoding.UTF8.GetBytes(postStr) 这一步。
@Launcher: (⊙o⊙)…,好吧,我以为它不会!对这些真是一点都不熟悉,谢谢
@Launcher: 最终确定为这样的:
public static string HttpPost(string Url, string Sign,string Timesp,string Non,string postXml) { string postData = string.Format("?Sign={0}&Timesp={1}&Non={2}",Sign, Timesp, Non); // 其他参数 //请求 request = (HttpWebRequest)WebRequest.Create(Url +postData); request.Method = "POST"; request.ContentType = "application/xml"; request.ContentLength = Encoding.UTF8.GetByteCount(postXml); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postXml); myStreamWriter.Close(); //读取 response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
@魔女小溪: 对
@Launcher: 恩恩,非常感谢,到时候测试一下看可以不可以,O(∩_∩)O~,我很笨吧!~~~~(>_<)~~~~
@魔女小溪: 你很聪明
@Launcher: 大神,我差点都让你吐血了,O(∩_∩)O哈哈~,话说,大神,你以后还是不要随便要别人的电话的好,这样不安全,万一我是骗子怎么办!O(∩_∩)O哈哈~
@魔女小溪: url传参......话说这哪长的像接口
@iEvent: (⊙o⊙)…,是我表达错误了,O(∩_∩)O哈哈~
@Launcher: 出现这个错误是啥意思?The request was aborted: The request was canceled.请求被取消,呜呜
@魔女小溪: 哪儿出现这个错误?
@Launcher: 就是请求的时候
@Launcher: 昨天没测试,今天我测试了一下,结果就报出这样的错误了
@魔女小溪: 你先用 Fiddler 测试下,
POST http://sssss.ss.ss/moxq.aspx?Sign=ssouwer&Timesp=sssss&Non=23434 HTTP/1.1 User-Agent: Fiddler Host: sssss.ss.ss Content-Type: application/xml; charset=utf-8 Content-Length: 213 <xml> <ToUserName><![CDATA[{0}]]></ToUserName> <FromUserName><![CDATA[{1}]]></FromUserName> <CreateTime>{2}</CreateTime> <MsgType><![CDATA[{3}]]></MsgType> <Content><![CDATA[{4}]]></Content> </xml>
这只是一个请求的模板,根据你的具体需求,将参数修改为正确的值。
@Launcher: 好的
@Launcher: 是这样嘛?
ServicePointManager.DefaultConnectionLimit = 500; //string postData = string.Format("?Sign={0}&Timesp={1}&Non={2}",Sign, Timesp, Non); // 其他参数 string postData = "?Sign=58&Timesp=656&Non=5453"; //请求 request = (HttpWebRequest)WebRequest.Create(Url +postData); request.Method = "POST"; request.ContentType = "application/xml"; request.Timeout = 200000; request.ContentLength = Encoding.UTF8.GetByteCount(postXml); Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(postXml);
@魔女小溪: 你先用 Fiddler 测试下再说.
因为我实在是不知道你想说法,你没头脑的抛出一句 The request was aborted: The request was canceled,请问,这句话在哪里出现的?记事本?IDE 输出框?还是抛出的异常的描述信息?
@Launcher: 不懂fiddler,我只用过火狐的poster
@Launcher: 抛出来的异常,我用写入到记事本上面了,我正在安装fiddler
@魔女小溪: 有异常,你不会看下 stack 和 innerexception ?
@Launcher: 额,我不知道,怎么去看,因为我是在微信中,通过发送关键字,来触发这个post提交的
@Launcher: 呜呜呜,我笨!
@Launcher: 呵呵,错误来了,是我太笨了
System.IO.IOException: Cannot close stream until all bytes are written.
at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting) at System.Net.ConnectStream.CloseInternal(Boolean internalCall, Boolean aborting)
at System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(CloseExState closeState)
at System.Net.ConnectStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.StreamWriter.Dispose(Boolean disposing)
at System.IO.StreamWriter.Close()
at EasyWeixin.Helpers.NetworkHelperAll.HttpPost(String Url, String Sign, String Timesp, String Non, String postXml)
at EasyWeixin.Web.Framework.CommonService.CustomMessageHandler.CustomMessageHandler.callProxy(String proxy_url, String postStr)
@魔女小溪:
myStreamWriter.Write(postXml); 后 myStreamWriter.Flush();
检查下 request.ContentLength 的大小。
@Launcher: request.ContentLength=10
@Launcher: 错误:
at System.Net.HttpWebRequest.GetResponse()
at EasyWeixin.Helpers.NetworkHelperAll.HttpPost(String Url, String Sign, String Timesp, String Non, String postXml)
at EasyWeixin.Web.Framework.CommonService.CustomMessageHandler.CustomMessageHandler.callProxy(String proxy_url, String postStr)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Flush()
at EasyWeixin.Helpers.NetworkHelperAll.HttpPost(String Url, String Sign, String Timesp, String Non, String postXml)
at EasyWeixin.Web.Framework.CommonService.CustomMessageHandler.CustomMessageHandler.callProxy(String proxy_url, String postStr)
@魔女小溪: 用 Fiddler 或火狐的poster 测试过了吗?
@Launcher: 我刚刚去网上找了一段post提交的代码,粘贴到我的项目中,把之前写的注释了,结果,成功了,但是,之后又不成功了,而且还没有错误!莫名其妙呀!
@Launcher: 我想请问一下,就是,是不是post一次之后,如果在post的话,有啥要求呢?
@魔女小溪: 再 post ,就再 Create 一个 HttpWebRequest。
@Launcher: 请问一下,火狐的poster里面怎么添加 xml数据,我只知道添加参数
@Launcher: 我终于找到原因了,(⊙o⊙)…,就是
我把下面的参数值,直接写死他就成功,就是第二个,第一个是不行的,所以,但是,我传过来都是有值的,所以,应该是哪里拼接出问题了还是不知怎么的
1、string postData = string.Format("?Sign={0}&Timesp={1}&Non={2}",Sign, Timesp, Non); // 其他参数
2、string postData = "?Sign=58&Timesp=656&Non=5453";
@魔女小溪: 你检查这几个参数传入的值是多少,以及你最后 Url +postData 的值是多少。
@Launcher: 出了这样的错误:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at EasyWeixin.Helpers.NetworkHelperAll.HttpPost(String Url, String Sign, String Timesp, String Non, String postXml)
at EasyWeixin.Web.Framework.CommonService.CustomMessageHandler.CustomMessageHandler.callProxy(String proxy_url, String postStr)
@Launcher: 哈哈,大神,你骂我吧,是我太粗心了,参数放错位置了,参天呀,我怎么也是个女的呀!
http://blog.163.com/da7_1@126/blog/static/104072678201221443913452/
http://honda418.iteye.com/blog/337052/
我是c#的,不是java