我已经阅读了许多主题和论坛来帮助自己,但我没有找到问题
您可以在code及以下
查看我的代码如何在参数(如ID)中添加值并获得响应 使用此代码,我有一个“1.0.2错误参数,请正确使用”
如果我使用ETL并且我正在使用CURL调用它可以,但我想在C#中进行
我的卷曲参数的例子< -d'{\“id \”:[\“123456 \”]}'>
我试着在我的bye [] data = .....(postData + id)中添加“id” 其中id = [123456]或{[123456]}或& [123456]或:[123456]
我没有想法,有人可以帮助我吗? THX
try
{
// init data
string user = "user1";
string pass = "password1";
string url = "https://mysite/getdatas/";
// init lib
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = user + ":" + pass;
byte[] data = Encoding.GetEncoding("UTF-8").GetBytes(postData) ;
// lib parameters
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
request.Credentials = new NetworkCredential(user, pass);
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
// reponse > stream
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
// display stream ( response )
StreamReader sr = new StreamReader(stream);
Console.WriteLine(sr.ReadToEnd());
Console.ReadLine();
sr.Close();
stream.Close();
}
答案 0 :(得分:0)
我已经检查了您发布的图片代码。据我所知,您希望对现有的Web API进行API调用。以下链接可能有助于此目的。
我还为您准备了一个小例子,其中包括一个ASP.NET Web API和一个进行API调用的控制台应用程序。
Web API方面:
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace WebAPICall.Controllers
{
public class PeopleController : ApiController
{
List<Person> people = new List<Person> { new Person { Id = 1, Name = "John" }, new Person { Id = 2, Name = "Mary" } };
// GET api/People/1
public IHttpActionResult Get(int id)
{
Person person = people.FirstOrDefault(p => p.Id == id);
if (person == null)
{
return NotFound();
}
return Ok(person);
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
客户端:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace WebApiTest
{
class Program
{
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task RunAsync()
{
// Before using, "Install-Package Microsoft.AspNet.WebApi.Client"
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:56409/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Person person = null;
HttpResponseMessage response = await client.GetAsync("api/People/1");
if (response.IsSuccessStatusCode)
{
person = await response.Content.ReadAsAsync<Person>();
}
Console.WriteLine("Id: {0}, Person: {1}", person.Id, person.Name);
Console.ReadKey();
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
我已经测试了上面的代码并且它正在运行。 输出是:
Id: 1, Person: John
如果这是你要求的,请告诉我。
注意:要使用HttpClient,您需要在客户端项目中通过Nuget Package Manager安装Microsoft.AspNet.WebApi.Client。
答案 1 :(得分:0)
我不知道我是否遗漏了= =数组输入中的%/
用户:密码ID = [123456]
用户:密码%[123456]
如果我想要很多ID,[123456,789] ......