2017-01-05 11 views
2

Googleには、GETメソッドとPUTメソッドの両方を持つサードパーティAPIがあります。サードパーティAPIは応答を返し、XMLでのみ受け入れます。 APIは、それは私がサードパーティのAPIでのputメソッドを使用してABCDする広告-93からここで名前を変更する必要がHttpClient RestAsIを使用してフィールドを更新するPutAsync

<prj:prj uri="https://bh.org/api/v2/prj/V51" lid="V51" xmlns:udf="http://ge.com/ri/userdefined" xmlns:ri="http://ge.com/ri" xmlns:file="http://ge.com/ri/file" xmlns:prj="http://ge.com/ri/prj"> 
<name>fgfgfg</name> 
<res uri="https://bh.org/api/v2/res/19"/> 
<udf:type name="cis"/> 
<udf:field type="String" name="ST">Cli</udf:field> 
<udf:field type="String" name="CPN">TestName</udf:field> 
<udf:field type="Numeric" name="No">1</udf:field> 
<udf:field type="String" name="CA">Do not know</udf:field> 
<udf:field type="String" name="Cto">Me</udf:field> 
<udf:field type="String" name="Site">GT</udf:field> 
</prj:prj> 

を返すGETとhttps://bh.org/api/v2/prj/A152のように見えます。私たちは、上記のコード私は、応答として取得XDocumentに名前の値を変更することができAMで応答

using (var client_Name = new HttpClient()) 
{ 
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate { return true; }); 
    Uri uri = new Uri(BaseURL_C); 
    client_Name.BaseAddress = uri; 
    client_Name.DefaultRequestHeaders.Accept.Clear(); 
    client_Name.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); 
    client_Name.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray_C)); 

    string c_URL = BaseURL_C + "api/v2/prj/" + Name; 
    var response_LabURL = client_Name.GetAsync(c_URL).Result; 
    string responseString_URL = response_LabURL.Content.ReadAsStringAsync().Result; 
    XDocument new_doc = XDocument.Parse(responseString_URL); 
    new_doc.Descendants("name").FirstOrDefault().Value = serviceResponse; 

を取得するにはGETメソッドを使用して、サードパーティのAPIを呼び出すアプリケーションを作成しました。これで、XDocumentをputAsyncのパラメータとして渡して、Rest APIを使用してフィールドをアップアップしようとしています。

using (var putClient = new HttpClient()) 
{ 
var requestUrl = string c_URL = BaseURL_C + "api/v2/prj/" + Name;; 
using (HttpContent httpContent = new XDocument(new_doc)) 
    { 
    httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); 
    HttpResponseMessage response = httpClient.PutAsync(requestUrl, httpContent).Result; 
    } 

しかし、上記のコードはCannot implicitly convert type 'System.Xml.Linq.XDocument' to 'System.Net.Http.HttpContent'

ように私はそうパラメータとして渡す方法HtppContentにしてXDocumentのnew_docを転化する方法がわからないエラーがスローされます。

答えて

3

あなたはこの

HttpContent httpContent = new StringContent(new_doc.ToString(), Encoding.UTF8, "application/xml"); 

のようにそれに何かを使用して行を削除する必要が

httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml"); 
関連する問題