C#打开php链接传参然后接收返回值

时间:2024-10-31 00:03:50

php代码

一。php

<?php
header("Content-Type:text/html;charset=UTF-8");
$u=$_POST['zdupdate'];
$p=$_POST['pid'];
$a=$_POST["afid"];
$d=$_POST["dtime"];

require('../db/conn.php');//打开文件
$sql_expire="insert into 表(aff_id,p_id,log_date,create_date) values($a,$p,'$d',now())";

if($u=='Y')
{
$myconn = mysql_connect($server_name, $db_username, $db_password);
mysql_select_db($db_name);
mysql_query("set names 'utf8'");
mysql_query($sql_expire);
print "1HHhh..1154QQwweeWW";

}

?>

二。C#代码

string postString = "zdupdate=Y&pid=" + dt.Rows[i]["Pid"].ToString() + "&afid=" + dt.Rows[i]["affiateid"].ToString() + "&dtime=" + dt.Rows[i]["Dtime"].ToString() + "";//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
string url = "http://haibook.pnxchina.com/api/index.php";//地址
WebClient webClient = new WebClient();
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流

string srcString = Encoding.UTF8.GetString(responseData);//解码