关于HttpWebRequest上传文件的问题

灰太狼已不再单身 2014-06-11 04:20:17
现在,我们要向服务器上传ZIP或xml文件,使用HttpWebRequest 或webclient,但是,目前我没有成功。
服务器端的是java代码,upload参数是必须的,但是,这个参数,怎么加到我的asp.net里面啊,求教。
上传代码如下:
调用方法代码

string posturl = "http://jshz.sme.gov.cn/ws/receive.action";
string para = "username=" + username + "&pwd=" + pwd;
byte[] buffer = null;
using (FileStream fileStream = new FileStream(path + "\\" + fname, FileMode.Open, FileAccess.Read))
{
buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
}
responseData = HttpHelper.PostFile(loginurl, para, "application/x-www-form-urlencoded", buffer, fname, "application/x-www-form-urlencoded", posturl);



/// <summary>
/// 利用HttpWebRequest上传文件
/// </summary>
/// <param name="loginurl">登录验证接口URL</param>
/// <param name="loginbody"></param>
/// <param name="logincontentType"></param>
/// <param name="filebyte">文件字节</param>
/// <param name="filename">文件名称</param>
/// <param name="filetype">文件类型PostedFile.ContentType</param>
/// <param name="posturl">接收文件的接口URL</param>
/// <returns></returns>
public static string PostFile(string loginurl, string loginbody, string logincontentType, byte[] filebyte, string filename, string filetype, string posturl)
{
//获取要上传的文件信息
byte[] data = filebyte;
string fileName = filename;
string fileType = filetype;
string fileSize = data.Length.ToString();

HttpWebRequest myRequest = null;

#region 登录
myRequest = (HttpWebRequest)WebRequest.Create(loginurl);
myRequest.ContentType = logincontentType;
myRequest.Method = "POST";
myRequest.Timeout = 1000 * 60;
myRequest.CookieContainer = mycookie;

byte[] btBodys = Encoding.UTF8.GetBytes(loginbody);
myRequest.ContentLength = btBodys.Length;
myRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);

HttpWebResponse httpWebResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
string responseContent = streamReader.ReadToEnd();

httpWebResponse.Cookies = mycookie.GetCookies(myRequest.RequestUri);//获取一个包含url的cookie集合的cookiecollection

#endregion

//上传文件
myRequest = (HttpWebRequest)WebRequest.Create(posturl);
myRequest.Method = "POST";
myRequest.ContentType = fileType;
myRequest.ContentLength = data.Length;
myRequest.CookieContainer = mycookie;
myRequest.Headers.Add("FileType", HttpContext.Current.Server.UrlEncode(fileType));
myRequest.Headers.Add("FileSize", fileSize);
myRequest.Headers.Add("FileName", HttpContext.Current.Server.UrlEncode(fileName));

using (Stream newStream = myRequest.GetRequestStream())
{
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
}

// Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();

return content;
}

图片是java上传的实例
...全文
203 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hb1122 2014-06-12
  • 打赏
  • 举报
回复
引用 5 楼 lifueng 的回复:
[quote=引用 4 楼 hb1122 的回复:]


string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes("upload=" + postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }
试试
OK,我试试,刚才我问了对方,对方也说 upload 是上传的文件流[/quote] 结贴给分啊,火火火
  • 打赏
  • 举报
回复
引用 7 楼 hb1122 的回复:
[quote=引用 5 楼 lifueng 的回复:] [quote=引用 4 楼 hb1122 的回复:]


string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes("upload=" + postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }
试试
OK,我试试,刚才我问了对方,对方也说 upload 是上传的文件流[/quote] 结贴给分啊,火火火[/quote] 上传还是不成功,一直提示参数错误
  • 打赏
  • 举报
回复
引用 4 楼 hb1122 的回复:


string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes("upload=" + postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }
试试
OK,我试试,刚才我问了对方,对方也说 upload 是上传的文件流
hb1122 2014-06-11
  • 打赏
  • 举报
回复


string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes("upload=" + postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }
试试
hb1122 2014-06-11
  • 打赏
  • 举报
回复


string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes(upload + postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }

试试
by_封爱 2014-06-11
  • 打赏
  • 举报
回复

string SendPostRequest(string url, string postString)
    {
        byte[] postData = Encoding.UTF8.GetBytes(postString);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        byte[] responseData = client.UploadData(url, "POST", postData);
        return Encoding.UTF8.GetString(responseData);
    }

Response.Write(SendPostRequest("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getSupportCity", "byProvinceName=辽宁"));
看下这个 这是请求服务 参数byProvinceName是辽宁 可以正确post给server 不知道对你有没有帮助..
  • 打赏
  • 举报
回复
我现在上传提示错误:参数错误。。。然后,问了对方,那人说 upload参数是必须的,我就不知道这个参数是加到哪儿的

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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