• 全部
  • C#综合技术
  • C#互联网桌面应用
  • AppLauncher
  • WinForm&WPF
  • C#开发新技术
  • 问答

提问:如何用C#做HTTP的POST方法请求

boymaster 2005-12-28 10:35:12
本人最初刚开始接触,请大虾们帮帮忙

C#如何用HTTP以text/plain方式来POST一个xml文件的内容!!!

在线期待中……
...全文
837 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
heilong05 2006-06-30
Encoding encoding = Encoding.GetEncoding("GB2312");
string strUrl = @"http://" + DefaultIP + "" + DefaultUrl + "";
string postData ="username="+UserName+"&password="+password+"&area_c=&telfile1="+TelePhones+"";
byte[] data = encoding.GetBytes(postData);
// 准备HTTP请求...
System.Net.HttpWebRequest myRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strUrl);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
System.IO.Stream newStream = myRequest.GetRequestStream();
// 发送数据
newStream.Write(data, 0, data.Length);
newStream.Close();
//HTTP响应
System.Net.HttpWebResponse myResponse = (System.Net.HttpWebResponse)myRequest.GetResponse();
System.IO.Stream receiveStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, encode);
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
StringBuilder sb = new StringBuilder("");
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append(readstr);
count = readStream.Read(read, 0, 256);
}
myResponse.Close();
readStream.Close();



====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2

回复
mark
回复
blackhero 2006-06-30
http://www.manbu.net/Lib/Class1/Sub8/1/24.asp
回复
heilong05 2006-06-30
Mark
回复
dreadknight2 2006-02-15
学习学习
回复
Qqwwee_Com 2006-02-15
可以参考我尾巴后面的软件
有源代码的。



====CSDN 小助手 V2.5 2005年11月05日发布====
CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
下载:http://szlawbook.com/csdnv2

回复
jrl5365 2006-02-15
收下了!~以后可能用的着!
回复
ArLi2003 2006-02-15
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemNetWebClientClassUploadValuesTopic.htm

webclient 的 uploadXXXXXXXXXX 简单的很,不必webrequest
回复
cmHua 2006-02-14
收藏起来慢慢看
回复
singlepine 2005-12-29
http://singlepine.cnblogs.com/articles/292661.html
回复
boymaster 2005-12-29
HttpWebRequest req = (HttpWebRequest) WebRequest.Create ( LMMPUrl ) ;

LMMPUrl直接写URL吗?类似:http://www.csdn.net吗?
回复
atliu 2005-12-28
好像是孟子E章上的文章
回复
atliu 2005-12-28
public static string PostData( string str)
{
try
{
byte[] data = System.Text.Encoding.GetEncoding ("GB2312").GetBytes ( str ) ;
// 准备请求...
HttpWebRequest req = (HttpWebRequest) WebRequest.Create ( LMMPUrl ) ;
req.Method = "Post" ;
req.ContentType ="application/x-www-form-urlencoded";
req.ContentLength = data.Length ;
Stream stream = req.GetRequestStream () ;
// 发送数据
stream.Write ( data ,0 ,data.Length ) ;
stream.Close () ;

HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
Stream receiveStream = rep.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader( receiveStream, encode );

Char[] read = new Char[256];
int count = readStream.Read( read, 0, 256 );
StringBuilder sb = new StringBuilder ("") ;
while (count > 0)
{
String readstr = new String(read, 0, count);
sb.Append ( readstr ) ;
count = readStream.Read(read, 0, 256);
}

rep.Close();
readStream.Close();

return sb.ToString () ;

}
catch(Exception ex)
{
return "" ;
ForumExceptions.Log ( ex ) ;
}
}
回复
s5689412 2005-12-28
参考MSDN中HttpWebRequest 和 HttpWebResponse 类的内容。
回复
发帖
C#
创建于2007-09-28

10.5w+

社区成员

.NET技术 C#
申请成为版主
帖子事件
创建了帖子
2005-12-28 10:35
社区公告

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