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

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

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

在线期待中……
...全文
989 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
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

固执的染色体 2006-06-30
  • 打赏
  • 举报
回复
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 类的内容。

111,097

社区成员

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

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

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