Home > Net >  About the HttpClient: the same code in different environmental results?
About the HttpClient: the same code in different environmental results?

Time:02-10

Code is very simple, it is through the HttpClient request a JSON, and back to the client, in order to solve the JSON cross-domain,

In the Program. The cs did not move, Startup. Cs is the following code, without adding any other documents and code:
 
Public void the Configure (IApplicationBuilder app, IWebHostEnvironment env)
{

App. UseRouting ();

HttpClientHandler handler=new ()
{
AutomaticDecompression=DecompressionMethods. GZip | DecompressionMethods. Deflate
};

HttpClient hc=new (handler);

App. UseEndpoints (endpoints=& gt;
{
Endpoints. MapGet ("/", async httpContext=& gt;
{
If (httpContext. Request. Query. Either ContainsKey (" url "))
{
The string url=httpContext. Request. Query (" url ");

HttpResponseMessage r=hc. GetAsync (url). The Result;
HttpContext. Response. ContentType=
(string) IsNullOrWhiteSpace (r.C ontent. Headers. ContentType. MediaType)? "Application/json" : r.C ontent. Headers, ContentType. MediaType)
+ "; The charset="+
(string) IsNullOrWhiteSpace (r.C ontent. Headers. ContentType. CharSet)? "Utf-8" : r.C ontent. Headers, ContentType. CharSet);

Await httpContext. Response. WriteAsync (await r.C ontent. ReadAsStringAsync ());
}
The else
{
Await httpContext. Response. WriteAsJsonAsync (new {err="empty url"});
}
});
});
}


The native environment: win10 v2004; The.net Core SDK 5.0.102
Server environment: Windows server 2012 r2; ASP.NET the Core Runtime 5.0.2

In the unit test is normal, the url parameter for a page, can return to normal the JSON data:
To the server, is still the page url parameters, returns the url generated page error page is not a JSON data, not the url to the server has limit, because on the server browser with direct access to the address, can return to normal the JSON data,

Access the url where the server is Apache Coyote/1.1

Is my code has a problem, or there is something wrong with the environment?

CodePudding user response:

Than your browser requests directly with HttpClient sends the request and the difference between, I estimate that big probability is appeared on the request of the head, especially the user-agent, the server-side code analysis of the head and you may have to use HttpClient request when no separate processing information of the head, so the browser can request that your code no, browser requests when is the head, before I met a similar problem

CodePudding user response:

Development communication program should start from capturing packet, use at least one HTTP, downlink message capture tool, of course, you must first learn, read the HTTP message,

CodePudding user response:

Early we use Fiddler as a necessary tool,
  • Related