C#用HttpWebRequest上传数据的问题。

来源:百度知道 编辑:UC知道 时间:2024/05/22 07:43:24
我写了下面一段代码。
哪些地方写的不太妥当?请高手给些建议。谢谢

/// <summary>
/// 数据上传到Web服务器
/// </summary>
/// <param name="address"></param>
/// <param name="data"></param>
/// <returns></returns>
private string UploadData(string address, string data)
{
string sReturnString = "";
HttpWebRequest httpReq = null;
WebResponse httpRespon = null;

// HttpWebRequest对象
httpReq = (HttpWebRequest)WebRequest.Create(new Uri(address));
httpReq.Method = "POST";
byte[] dataBytes = Encoding.UTF8.GetBytes(data);

//不使用缓存
httpReq.AllowWriteStreamBuffering = false;

//Timeout(5秒)
httpReq.Timeout = 5000;
httpReq.ContentType = "text/xml";
httpReq.ContentLength = dataBytes.Length;
httpReq.Headers = this.Headers;
using (Stream postStream = ht

Request可以当成全局变量同url一起传进来
在方法里只修改Reqest的访问地址就可以了
也就是不用没次都要给Request分配内存 你不是要循环的吗
其它的都没什么问题

你的说出你程序中的问题和想知道的东西,直接看你这个东西,有点累!