我搞了两天,用PHP去POST数据都不行,什么方法都用尽了.实在无力可发了           byte[] data = encoding.GetBytes(postData);
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                //request.ContentType = "application/x-www-form-urlencoded";
                request.ContentType = "INFOSEC_SIGN/1.0";
                request.Timeout = Setting.TimeOut;
                request.ContentLength = data.Length;                Stream stream = request.GetRequestStream();
                stream.Write(data, 0, data.Length);
                stream.Close();
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader read = new System.IO.StreamReader(response.GetResponseStream(), encoding))
                {
                    return read.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
我翻译的代码 function DoPost($posturl,$parameters)
{

$ch = curl_init();
$responsecode=0;
$ErrorInfo='';
curl_setopt($ch, CURLOPT_URL,$posturl);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('INFOSEC_SIGN/1.0') );
curl_setopt($ch, CURLOPT_PORT, 449);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);  
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
$ret = curl_exec($ch);
var_dump($ret);
$responsecode= curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($ret == NULL) { 
   $ErrorInfo = "call http err :" . curl_errno($ch) . " - " . curl_error($ch) ;
   curl_close($ch);
   return false;
}
curl_close($ch);
return $ret;
}

解决方案 »

  1.   

    很抱歉,C#我学了一个月就彻底放弃了,不能帮你,给你发一个curl的例子你看看吧,或许对你有所帮助
    Set_time_limit(0);$cookie_jar = '/tmp/cookie.tmp';//cookie保存目录//模拟请求数据
    Function request($url,$postfields,$cookie_jar,$referer){
    $ch = curl_init();
    $options = array(CURLOPT_URL => $url,
    CURLOPT_HEADER => 0,
    CURLOPT_NOBODY => 0,
    CURLOPT_PORT => 80,
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => $postfields,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_COOKIEJAR => $cookie_jar,
    CURLOPT_COOKIEFILE => $cookie_jar,
    CURLOPT_REFERER => $referer
    );
    curl_setopt_array($ch, $options);
    $code = curl_exec($ch);
    curl_close($ch);
    Return $code;
    }//获取帖子列表
    Function getThreadsList($code){
    Preg_match_all('/<!--[.|\r|\n]*?<a href=\"viewthread.php\?tid=(\d+)/',$code,$threads);
    Return $threads[1];
    }//判断该帖子是否存在
    Function isExits($code){
    Preg_match('/<p>指定的主题不存在或已被删除或正在被审核,请返回。<\/p>/',$code,$error);
    Return isset($error[0])?false:true;
    }//获取帖子标题
    Function getTitle($code){
    Preg_match('/<h1>[<\/h1>]*/',$code,$title_tmp);
    $title = $title_tmp[0];
    Return $title;
    }//获取帖子作者:
    Function getAuthor($code){
    Preg_match('/<a href=\"space.php\?uid=\d+\" target=\"_blank\" id=\"userinfo\d+\" onmouseover=\"showMenu\(this\.id\)\">.+/',$code,$author_tmp);
    $author = strip_tags($author_tmp[0]);
    Return $author;
    }//获取楼主发表的内容
    Function getContents($code){
    Preg_match('/<div id=\"postmessage_\d+\" class=\"t_msgfont\">(.|\r|\n)*?<\/div>/',$code,$contents_tmp);
    $contents = preg_replace('/images\//','http://bbs.war3.cn/images/',$contents_tmp[0]);
    Return $contents;
    }//打印帖子标题
    Function printTitle($title){
    Echo "<strong><h2>帖子标题:</h2></strong>",strip_tags($title),"<br/><br/>";
    }//输出帖子作者
    Function printAuthor($author){
    Echo "<strong><h2>帖子作者:</h2></strong>",strip_tags($author),"<br/><br/>";
    }//打印帖子内容
    Function printContents($contents){
    Echo "<strong><h2>作者发表的内容:</h2>",$contents,"</strong><br/>";
    }//错误
    Function printError(){
    Echo "<i>该帖子不存在!</i>";
    }
    /*函数列表end---------------------------------------------------------------------------------------------------*//*登录论坛 begin*/
    $url = 'http://bbs.war3.cn/logging.php?action=login';
    $postfields='loginfield=username&username=1nject10n&password=xxxxxx&questionid=0&cookietime=315360000&referer=http://bbs.war3.cn/&loginsubmit=提交';
    request($url,$postfields,$cookie_jar,'');
    unset($postfields,$url);
    /*登录论坛 end*//*获取帖子列表(位于第一页的帖子) begin*/
    $url = 'http://bbs.war3.cn/forumdisplay.php?fid=57';
    $code = request($url,'',$cookie_jar,'');
    $threadsList = getThreadsList($code);
    /*获取帖子列表 end*///帖子序列
    $rows = 0;/*循环抓取所有帖子源代码 begin*/
    Foreach($threadsList as $list){
    $url = "http://bbs.war3.cn/viewthread.php?tid=$list"; IF(isExits($code)){
    $code = request($url,'',$cookie_jar,'');
    $color = $rows%2==0?'#00CCFF':'#FFFF33';
    Echo "<div style='background-color:$color'>";
    Echo "<h1>第",($rows+1),"贴:</h1><br/>";
    $author = getAuthor($code);
    printAuthor($author);
    $title = getTitle($code);
    printTitle($title);
    $contents = getContents($code);
    printContents($contents);
    Echo "</div>";
    $rows++;
    }Else{
    printError();
    }
    Echo "-----------------------------------------------------------------------------------------<br/><br/>";
    }
    /*抓取源代码 end*/
      

  2.   

    楼主是要用php访问c#,还是用php实现那段c#的功能?