62,041
社区成员
发帖
与我相关
我的任务
分享
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;
}
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);
}
试试
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);
}
试试
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 不知道对你有没有帮助..