localhost에서 Visual Studio 2012를 사용하여 ASP.NET 웹 API 서비스에 연결하는 방법을 배우고 있습니다.웹 API GetAsync가 로컬 호스트에서 작동하지 않습니다.
namespace ProductStore.Controllers
{
public class ProductsController : ApiController
{
static readonly IProductRepository repository = new ProductRepository();
public IEnumerable<Product> GetAllProducts()
{
return repository.GetAll();
}
public Product GetProduct(int id)
{
Product item = repository.Get(id);
if (item == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return item;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return repository.GetAll().Where(
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
}
public HttpResponseMessage PostProduct(Product item)
{
item = repository.Add(item);
var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
string uri = Url.Link("DefaultApi", new { id = item.Id });
response.Headers.Location = new Uri(uri);
return response;
}
public void PutProduct(int id, Product product)
{
product.Id = id;
if (!repository.Update(product))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
public void DeleteProduct(int id)
{
repository.Remove(id);
}
}
}
나는 다음과 같은 코드를 사용하여이 웹 API에 연결을 시도하고있다 :
static async Task RunAsyncGet()
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:9000/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("/api/product/1");
if (response.IsSuccessStatusCode)
{
Product product = await response.Content.ReadAsAsync<Product>();
Console.WriteLine("{0}\t${1}\t{2}", product.Name, product.Price, product.Category);
}
}
}
catch (Exception ex)
{
throw;
}
}
나는이 (가)의 App.config에 다음과 같은 한 (여기
샘플 웹 API 컨트롤러입니다 이 온라인을 찾았습니다.) :<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>
이 줄을 실행하면 응용 프로그램이 실행을 중지합니다.
HttpResponseMessage response = await client.GetAsync("api/products/1");
무엇이 원인입니까? 가능한 몇 가지 문제가 있습니다
System.Net.Http.HttpRequestException was caught HResult=-2146233088 Message=An error occurred while sending the request. Source=mscorlib StackTrace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ProductStoreClientFormsApplication.Form1.<RunAsyncGet>d__0.MoveNext() in h:\Learning\WEB API\ProductStoreClientFormsApplication\ProductStoreClientFormsApplication\Form1.cs:line 33 InnerException: System.Net.WebException
HResult=-2146233079
Message=The underlying connection was closed: Unable to connect to the remote server.
Source=System
StackTrace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
InnerException: System.Net.Sockets.SocketException
HResult=-2147467259
Message=An invalid argument was supplied
Source=System
ErrorCode=10022
NativeErrorCode=10022
StackTrace:
at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
InnerException:
방금 갈 때 무슨 일이 발생합니까? 브라우저에서'http : // localhost : 9000/api/product/1'? –
http : // localhost : 9000/api/products/1은 올바른 정보를 반환합니다. – user2985419
은 "/ api/product/1"입니다.네가 여분의 "/"를 가지고있는 것처럼 나에게 보인다. 보십시오 "api/products/1"편집 : 방금 메인 블록에 귀하의 uri가 아래에 다른 것으로 나타났습니다. – failedprogramming