我想发布到Asp.Net WebApi Post方法:
// POST /api/citycode
public HttpResponseMessage<CityCode> Post(CityCode citycode)
{
try
{
Test.SelfTrackingEntities.BusinessLayer.BusinessManagers.CityCodeManager myCityCodeManager = new CityCodeManager(Utility.GetConnectionString());
var id = myCityCodeManager.Create(citycode);
var response = new HttpResponseMessage<Test.SelfTrackingEntities.BusinessLayer.BusinessEntities.CityCode>(citycode) { StatusCode = HttpStatusCode.Created };
response.Headers.Location = new Uri(VirtualPathUtility.AppendTrailingSlash(Request.RequestUri.ToString()) + citycode.Name);
return response;
}
catch (Exception e)
{
var response = new HttpResponseMessage(HttpStatusCode.Conflict);
response.Content = new StringContent(e.Message);
throw new HttpResponseException(response);
}
}
Run Code Online (Sandbox Code Playgroud)
客户电话是:
var objectContent = CreateJsonObjectContent(citycode);
objectContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var requestMessage = new HttpRequestMessage<CityCode>(citycode, jsonMediaType);
return httpClient.PostAsync(addressSuffix, objectContent).ContinueWith(task =>
{
var …Run Code Online (Sandbox Code Playgroud) 我已经阅读Atlassian的一个答案https://answers.atlassian.com/questions/79902/using-httpclient-c-to-create-a-jira-issue-via-rest-generates-bad-request-response其中一个用户通过以下代码创建了JIRA问题.我修改了它,但通过使用自构建类问题得到错误ObjectContent
Http.HttpContent content = new Http.ObjectContent<Issue>(data, jsonFormatter);
Run Code Online (Sandbox Code Playgroud)
编译器不会接受它.有人知道为什么吗?
public string CreateJiraIssue()
{
string data= @"{ ""fields"": {
""project"":
{
""key"": ""HELP""
},
""summary"": ""Test Ticket"",
""description"": ""Creating of an issue using project keys and issue type names using the REST API"",
""issuetype"": {
""name"": ""Ticket""
},
""assignee"": { ""name"": ""user"" }
}
}";
string postUrl = "https://xxx.jira.com/rest/api/2/";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.BaseAddress = new System.Uri(postUrl);
byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); …Run Code Online (Sandbox Code Playgroud) 我试图围绕着这个行为HttpClient.Timeout.MSDN将其描述为"在请求超时之前等待的毫秒数",但我并不完全清楚是什么意思request,或者如果我想要更具体的控制该怎么做.
我发现的一件事是,超时至少考虑了发送请求所需的时间,这有点令人沮丧,因为我正在使用足够大的数据,默认的100秒超时不足以发送整个内容.
一个简单的解决方案可能是大幅增加超时,但我很惊讶这可能会产生一些非常不利的后果.假设客户端无法与服务器建立连接.等待20分钟建立连接是荒谬的,但等待20分钟,而转移正在稳步推进是完全合理的(至少我的情况).
有没有办法让我更精细地控制超时元素HttpClient?可能我必须将超时设置为无限制,并通过CancellationToken其他机制使用我自己的代码管理超时?如果我需要这种程度的控制,我最好放弃HttpClient完全放弃,然后深入到底层HttpWebRequest和HttpWebResponse类?
有没有办法,使用HttpClient来区分"当我们没有得到服务器的响应"超时时间超出"超时"操作?
让我解释一下我们的问题:
有没有办法使用.NET HttpClient类来处理这种情况?从我测试中指定HttpClient上的TimeOut将为case1和case2设置相同的超时时间.
当我在控制台应用程序中调用下面的函数时,它只是工作.但是当我将它添加到MVC控制器时,执行永远不会到达JsonConvert行.任何想法,我错过了什么.
调用代码
GetVersion(url).Result.FileVersion
Run Code Online (Sandbox Code Playgroud)
方法
public static async Task<Version> GetVersion(string url, string hostHeader)
{
var client = new HttpClient();
if (!string.IsNullOrEmpty(hostHeader))
{
client.DefaultRequestHeaders.Host = hostHeader;
}
var version = await client.GetStringAsync(url);
var output = JsonConvert.DeserializeObject<Version>(version);
client.Dispose();
return output;
}
Run Code Online (Sandbox Code Playgroud) 这可能与以下问题相同:
使用MonoTouch,HttpClient和Charles Proxy时出现HTTP流量监控问题
但我想我会从Windows Azure移动服务客户端的角度来问它.当我将iOS设备配置为指向fiddler(在我的Windows框中)或查尔斯(在我的Mac上)时,我可以看到来自浏览器或使用WebRequest的Xamarin应用程序的所有流量.但是,如果我直接使用Microsoft.WindowsAzure.MobileServices.MobileServiceClient(使用HttpClient)或HttpClient,则流量不会显示.
我试着看:
http://fiddlerbook.com/fiddler/help/hookup.asp#Q-DOTNET
对于一些建议,但我不太了解在Xamarin.IOS中HttpClient的实现,以了解它们是否合适.即我似乎无法使用GlobalProxySelection,如下所述:
GlobalProxySelection.Select = new WebProxy("127.0.0.1", 8888);
Run Code Online (Sandbox Code Playgroud)
我不认为app.config建议使用:
<defaultProxy>
<proxy bypassonlocal="false" usesystemdefault="true" />
</defaultProxy>
Run Code Online (Sandbox Code Playgroud)
适用于Xamarin应用程序.对我来说,一个很好的出发点是了解HttpClient对WebRequest的不同做法,以便跟踪问题,但希望有人可以告诉我需要做些什么来解决这个问题.
谢谢
F
注意:我没有使用本地地址,因此不会使用本地地址.注意:这可以通过在配置为使用fiddler/charles作为代理的iOS设备上运行http://docs.xamarin.com/samples/HttpClient/并观察HttpClient流量未显示来轻松测试.
httpwebrequest xamarin.ios dotnet-httpclient azure-mobile-services
我正在尝试解析wiki空间页面,但我无法获得实际页面.我不确定它是否是一个HttpClient错误或一些丢失的配置.
这是我的代码:
HttpClientHandler handler = new HttpClientHandler();
handler.AllowAutoRedirect = true;
_httpClient = new HttpClient(handler);
HttpResponseMessage response = await _httpClient
.GetAsync("http://medivia.wikispaces.com/Monsters");
Run Code Online (Sandbox Code Playgroud)
当我运行该代码时,我得到StatusCode 302并被发送到https://session.wikispaces.com/1/auth/auth?authToken=token.我希望HttpClient能够跟随302,因为我有AllowAutoRedirect = true.
这是我第一次遇到这个问题.它适用于Postman和RestClient,它是RestSharp的一部分.
我正在使用C#HttpClient来模拟从服务器下载CSV文件的请求.我需要根据我下载的上一个文件的LastModified日期检查文件的LastModified日期,以检查文件是否已更改.
我发出请求时,HttpClient会获取HttpResponseMessage,但每次检查时都会
response.Headers.Date.Value
Run Code Online (Sandbox Code Playgroud)
我得到了请求的当前日期/时间.我理解,如果我请求文件的页面是由DB或其他动态方法生成的,则LastModified值将是请求发出的时间.
但是,我使用较旧的HttpWebRequest/Response尝试了相同的过程,我发现了
response.Headers[HttpResponseHeader.LastModified]
Run Code Online (Sandbox Code Playgroud)
将返回文件上次更改的日期,例如,2种不同的方法返回不同的日期,HttpWebResponse给出日期为1/12/2017,HttpResponseMessage给出日期为2017年3月30日.
如何使用HttpWebResponse获取文件更改日期?
我在一个IIS反向代理后面的Kestrel中运行了一个简单的asp.net core 2.0 WebApi应用程序.单个路由非常简单,它只是发出一个http请求:
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://www.google.ca");
HttpResponseMessage response = await client.GetAsync("/search?q=test&oq=test");
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
但是,此请求始终需要2-3秒才能完成.然而,如果我在没有IIS的情况下直接运行Kestrel,相同的代码需要大约100ms.
我按照所有Microsoft 文档来配置使用IIS的kestrel.
iis reverse-proxy dotnet-httpclient kestrel-http-server asp.net-core-2.0
我没有找到有关它的文档,我认为我当前的解决方案不是最佳的,因为处理程序生命周期不是由HttpClientFactory管理的:
var proxiedHttpClientHandler = new HttpClientHandler() { Proxy = httpProxy };
_createHttpClient = () => HttpClientFactory.Create(proxiedHttpClientHandler);
Run Code Online (Sandbox Code Playgroud)
有更好的解决方案吗?
c# ×6
.net ×5
.net-core ×2
asp.net ×1
asp.net-mvc ×1
date ×1
iis ×1
jira ×1
rest ×1
xamarin.ios ×1