php 发送c#,帮助将PHP方法转换为C#

有人能帮忙吗?我有一个php方法发送一个http post:

// API URL

$api_url = "url_of_the_API ";

// Apache auth

$api_user = "user";

$api_pass = "pass";

// Request

$xml = "";

$context = stream_context_create(

array (

'http' => array(

'method' => 'POST',

'header' => sprintf("Authorization: Basic %s\r\n",

base64_encode($api_user.':'.$api_pass))

."Content-type: application/x-www-form-urlencoded\r\n",

'content' => http_build_query(array('xml' => $xml)),

'timeout' => 5,

),

)

);

$response = file_get_contents($api_url,false,$context);

header ("Content-Type:text/xml");

echo $response;

?>

我需要将其转换为C,并且我从WebService得到一个回复,说“需要XML的后调用”。我被告知:

"the XML is not being sent via POST in the 'xml' (name) variable? This is a similar process to submitting a webform with one variable called 'xml'."

下面是我的C

// this is where we will send it

const string uri = "theUri";

// create a request

var request = (HttpWebRequest)

WebRequest.Create(uri); request.KeepAlive = false;

request.ProtocolVersion = HttpVersion.Version10;

request.Method = "POST";

request.Credentials = new NetworkCredential("theUserName", "thePassword");

// turn our request string into a byte stream

var postBytes = Encoding.ASCII.GetBytes(postData);

// this is important - make sure you specify type this way

request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = postBytes.Length;

var requestStream = request.GetRequestStream();

// now send it

requestStream.Write(postBytes, 0, postBytes.Length);

requestStream.Close();

// grab te response and print it out to the console along with the status code

var response = (HttpWebResponse)request.GetResponse();

Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());

Console.WriteLine(response.StatusCode);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值