there are numerous errors in your code:1. you should not do HttpUtility.UrlEncodeToBytes(data,System.Text.Encoding.Default ); you should encode the name part and value part separately, in this case, you do not need to encode2. you need to set ContentType and ContentLength3. you should close the request stream sso basically, you should do something likeString data ="name=liang"; byte[] buffer = System.Text.Encoding.Default.GetBytes(data);HttpWebRequest request= (HttpWebRequest)WebRequest.Create("http://www.net.cn");request.Method ="POST"; request.ContentLength = buffer.Length; request.ContentType = "application/x-www-form-urlencoded";Stream s = request.GetRequestStream(); s.Write(buffer,0,buffer.Length ); s.Close();HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.Default ); this.textBox1.Text = reader.ReadToEnd(); reader.Close(); response.Close();
我代码是: String data ="name=liang";
byte[] buffer = HttpUtility.UrlEncodeToBytes(data,System.Text.Encoding.Default );
HttpWebRequest request= (HttpWebRequest)WebRequest.Create("http://www.net.cn");
request.Method ="POST";
Stream s = request.GetRequestStream();
s.Write(buffer,0,buffer.Length );
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.Default );
this.textBox1.Text = reader.ReadToEnd();
但是用request.form()读的数据确实空字符传我用get方法可以把数据发送过去,用request()也能收到,但是我用urlEncode把客户端的数据encode后,用request()又不能取到数据了
HttpUtility.UrlEncodeToBytes(data,System.Text.Encoding.Default );
you should encode the name part and value part separately, in this case, you do not need to encode2. you need to set ContentType and ContentLength3. you should close the request stream sso basically, you should do something likeString data ="name=liang";
byte[] buffer = System.Text.Encoding.Default.GetBytes(data);HttpWebRequest request= (HttpWebRequest)WebRequest.Create("http://www.net.cn");request.Method ="POST";
request.ContentLength = buffer.Length;
request.ContentType = "application/x-www-form-urlencoded";Stream s = request.GetRequestStream();
s.Write(buffer,0,buffer.Length );
s.Close();HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),System.Text.Encoding.Default );
this.textBox1.Text = reader.ReadToEnd();
reader.Close();
response.Close();