PHP代码:include("db.php");
include("functions.php");
extract($_GET);
$errno=-1;
if($username==null||$password==null||$logdate==null||$sign==null)
$errno=5;
else
{
$flag=is_sign_ok($username,$password,$logdate,$sign);
if($flag==1) //sign ok
{
$sql="select id from ms_user where username='$username' and password='$password'";
$result=mysql_query($sql);
$num=mysql_num_rows($result);
if($num==0)
$errno=0;//login error
else
{
$row=mysql_fetch_array($result);
$path="files/backup/".$row['id'].".dat";
if(is_uploaded_file($_FILES['path']['tmp_name']))
{
   if(!move_uploaded_file($_FILES['path']['tmp_name'],$path))
      $errno=7;//upload error
}
else
$errno=7;//upload error
}
}
else if($flag==0)
$errno=3;
else if($flag==-1)
$errno=4;
}
if($errno!=-1)
$xml_str=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<content>
<status>FAIL</status>
<errno>$errno</errno>
</content>
";
else
$xml_str=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<content>
<status>SUCC</status>
</content>
";
echo $xml_str;
?>
网页接口: 
http://192.168.0.133/mobilespirit/backup.php?username=&password=&logdate=&sign=post 文件备份数据
其中:
所有GET参数都为utf8编码,并做urlencode
Sign由几个字段构成,然后做md5,用于校验用户数据,必须严格按顺序构成:
Md5(Username+password+logdate)C# 端去上传文件
//如何把 uploadFileName文件
        public bool UserBackupToNet(User user, string uploadFileName)
        {
            string tempUri = "http://192.168.0.133/mobilespirit/backup.php?username={0}&password={1}&logdate={2}&sign={3}";
///构造出uri
            string uri = this.InitUserLogInURI(user, tempUri);
}
我该如何把 需要上传的文件数据 给上传到服务器上了?这个不是上传文件的
还需要什么   html的form标签里要写enctype="multipart/form-data"才能post文件该怎么构造  
WebClient webClient = new WebClient();
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";                responseBytes = webClient.UploadData(uploadUrl, bytes);
里的   bytes  和   headers了

解决方案 »

  1.   

    http://blog.csdn.net/wo789/archive/2008/08/20/2800323.aspx
      

  2.   

    终于找到解决办法了。在网站上找了很多有关  c#模拟post 上传文件方法都是对的,但是由于是跟php 这边的接口没有配合好网上大部分能使用的代码:public WebResponse SendFile(string fileName, Uri uri)
            {            CookieContainer cookies = new CookieContainer();
                // cast the WebRequest to a HttpWebRequest since we're using HTTPS 
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
                httpWebRequest.CookieContainer = cookies;
                WebResponse webResponse = httpWebRequest.GetResponse();
                string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
                HttpWebRequest httpWebRequest2 = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest2.Credentials = CredentialCache.DefaultCredentials;
                httpWebRequest2.CookieContainer = cookies;
                httpWebRequest2.ContentType = "multipart/form-data; boundary=" + boundary;
                httpWebRequest2.Method = "POST";            // Build up the post message header 
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(boundary);
                sb.Append("--\r\n");
                sb.Append("Content-Disposition: form-data; name=\"file\"; filename=\"");
    //此行代码注意了,  name不一定是file  有可能是其他的值, 而我主要的问题是:在php中 用的path, 我一直传 file  当然一直传送不成功了
                //sb.Append("Content-Disposition: form-data; name=\"1\"; filename=\"");
                sb.Append(Path.GetFileName(fileName));
                sb.Append("\"");
                sb.Append("\r\n");
                sb.Append("Content-Type: application/octet-stream");
                sb.Append("\r\n");
                sb.Append("\r\n");            string postHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.Default.GetBytes(postHeader);
                //byte[] postHeaderBytes = DataConvert.StringToByte(postHeader);            // Build the trailing boundary string as a byte array 
                // ensuring the boundary appears on a line by itself 
                byte[] boundaryBytes = Encoding.Default.GetBytes("--" + boundary + "--\r\n");
                //byte[] boundaryBytes = DataConvert.StringToByte("\r\n--" + boundary + "\r\n");            FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
                httpWebRequest2.ContentLength = length;            Stream requestStream = httpWebRequest2.GetRequestStream();            // Write out our post header 
                requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);            // Write out the file contents 
                byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                    requestStream.Write(buffer, 0, bytesRead);            // Write out the trailing boundary 
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);            WebResponse webResponse2 = httpWebRequest2.GetResponse();
                return webResponse2;
            }  最后 可以参考如下网站 可以得到更好的代码整理的, 希望能帮到别人
    http://314858770.javaeye.com/blog/720456另外:如果真的还解决不了 推荐使用   微软的 网页监测工具: fiddler 去监测一下, 然后慢慢的去分析,总能解决问题的...
    没有解决不了的问题  只有 愿不愿意去解决而已。
    ....