簡體   English   中英

如何從C#Asp.Net方法發送HTTP Post?

[英]How can I send a HTTP Post from a C# Asp.Net method?

我想從我的C#方法中查詢字典API。 以下是所需規格:

POST /DictService/DictService.asmx/DefineInDict HTTP/1.1
Host: services.aonaware.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length

dictId=string&word=string

這是返回內容的示例,但是現在我使用“動盪”方式無法獲得任何返回值: http : //unirest.io/net.html

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<WordDefinition xmlns="http://services.aonaware.com/webservices/">
  <Word>string</Word>
  <Definitions>
    <Definition>
      <Word>string</Word>
      <Dictionary>
        <Id>string</Id>
        <Name>string</Name>
      </Dictionary>
      <WordDefinition>string</WordDefinition>
    </Definition>
    <Definition>
      <Word>string</Word>
      <Dictionary>
        <Id>string</Id>
        <Name>string</Name>
      </Dictionary>
      <WordDefinition>string</WordDefinition>
    </Definition>
  </Definitions>
</WordDefinition>

這是我到目前為止嘗試過的:

  HttpResponse<string> jsonResponse = Unirest.post("http://services.aonaware.com/DictService/DictService.asmx/DefineInDict")
                .header("Accept", "application/xml")
                .header("Content-Type", "application/x-www-form-urlencoded")
                .field("dictId", "wn")
                .field("word", "abandon")
                .asJson<string>();

總是我得到錯誤:

406-客戶端瀏覽器不接受所請求頁面的MIME類型。

有人可以幫忙,建議我如何發送此發帖請求。 也許有比使用Unirest容易的事情。 我願意接受任何簡單的C#解決方案

由於它是asmx,因此可以生成WSDL。

在此處輸入圖片說明

在此處輸入圖片說明

它將在“ Service References文件夾中生成以下文件。

在此處輸入圖片說明

然后您可以將其稱為

在此處輸入圖片說明

HelloWorld.asmx.cs

/// <summary>
/// Summary description for HelloWorld
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class HelloWorld : System.Web.Services.WebService
{

    [WebMethod]
    public string GetHelloWorld()
    {
        return "Hello World";
    }
}

僅供參考: asmx是一項非常古老的技術,已過時。 如果這是一個新應用程序,則可以考慮尋找REST。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM