如何在PHP中实现C#REST调用?

时间:2017-09-17 08:56:59

标签: c# rest post

如何将以下REST调用方法从PHP转换为C#?我是C#的新手,并学习如何进行Web API调用。

当我运行以下C#代码时,我收到了未经授权的错误。但在PHP中,它运行良好。

PHP代码:

$service_url = 'https://www.addresscope.com/api/v1/upas/get';

$ch = curl_init();
$auth = "Authorization: xxxx-xxxx-xxxx-xxxx"; 
curl_setopt($ch, CURLOPT_URL, $service_url);
$curl_post_data = array( 'upas' => array("UPA000000")); 
$curl_post_data = http_build_query($curl_post_data); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data); 
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array($auth)); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,  CURLOPT_SSL_VERIFYPEER , false);

$ch_result = curl_exec($ch);
if(curl_errno($ch)){  throw new Exception(curl_error($ch));  }
curl_close($ch);            

$curl_response = $ch_result;

$decoded = json_decode($curl_response); 
if (isset($decoded->status) && $decoded->status == 'error') 
{ 
  die('error occured: ' . $decoded->msg);     
} 
echo '<pre>';
echo 'response ok: '; var_dump($decoded);

C#代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Text;
    using System.IO;
    using Newtonsoft.Json;
    using System.Net.Http;

    .......
    .......
    .......

    var request =  
    (HttpWebRequest) 
    WebRequest.Create("https://www.addresscope.com/api/v1/upas/get");
    var postData = "upas=[UPA000000]";            
    var data = Encoding.ASCII.GetBytes(postData);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";

    request.Headers["Authorization"] = "xxxx-xxxx-xxxx-xxxx";
    request.ContentLength = data.Length;
    using (var stream = request.GetRequestStream())            {
        stream.Write(data, 0, data.Length);            }

    var response = (HttpWebResponse)request.GetResponse();
    var responseString = new 
    StreamReader(response.GetResponseStream()).ReadToEnd();
    TextBox1.Text = responseString;                 

1 个答案:

答案 0 :(得分:0)

我在完成http://codesamplez.com/programming/http-request-c-sharp这篇文章后能够提出请求。 我用它来准备我的课程。 为了读者的利益,我发布了在C#中发出REST请求所需的内容。 希望有人会觉得这很有用。

        var request = WebRequest.Create("https://www.addresscope.com/api/v1/upas/get");
        Stream dataStream;
        WebResponse response;
        String Status;
        string responseFromServer;
        string postData = "upas[]=UPA000000";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);            
        request.ContentType = "application/x-www-form-urlencoded";
        request.Headers.Add("Authorization", "xxxx-xxxx-xxxx-xxxx-xxxx");            
        request.ContentLength = byteArray.Length;
        request.Method = "POST";
        dataStream = request.GetRequestStream();            
        dataStream.Write(byteArray, 0, byteArray.Length);            
        response = request.GetResponse();
        Status = ((HttpWebResponse)response).StatusDescription;            
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);            
        responseFromServer = reader.ReadToEnd();            
        reader.Close();
        dataStream.Close();
        response.Close();
        TextBox1.Text = responseFromServer;