douzhongpi9734 2016-09-13 06:41
浏览 182
已采纳

带有标头字段的HTTP Post和C#中的XML数据

I am new to C# and i want to post a HTTP request to one specific URL along with the HTTP Headers and some XML data. I am having its PHP code but i don't know how to write the same in C#. My PHP code is as bellow.


$url ="https://datasend.getdata.com";
$session= $_REQUEST["session"];
$token= $_REQUEST["token"];

$XPost ="<ENVELOPE>
<HEADER>
<VERSION>1</VERSION>
<REQVERSION>1</REQVERSION>
<TALLYREQUEST>EXPORT</TALLYREQUEST>
<TYPE>DATA</TYPE>
<ID>TPGETCOMPANIES</ID>
<SESSIONID>$session</SESSIONID>
<TOKEN>$token</TOKEN>
</HEADER>
<BODY>
<DESC>
<STATICVARIABLES>
<SVINCLUDE>CONNECTED</SVINCLUDE>
</STATICVARIABLES>
</DESC>
</BODY>
</ENVELOPE>";

$headers = array();
$headers[] = 'ID:TPGETCOMPANIES';
$headers[] = 'SOURCE:MAZENETTECH';
$headers[] = 'TARGET: TNS';
$headers[] = 'CONTENT-TYPE:text/xml;charset=utf-8';
$headers[] = 'Accept-Encoding:identity';

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt($ch, CURLOPT_TIMEOUT, 40); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost); 
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch); 


echo($result);
  • 写回答

1条回答 默认 最新

  • douke6881 2016-09-13 06:52
    关注

    If you search there are so many answers on SO as well as other communities.

    HTTP post XML data in C#

    And much more.

    You can add predefined headers as well as custom header using the Request.Headers property.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含