Home > Net >  The console using HttpClient unable to get to the data server
The console using HttpClient unable to get to the data server

Time:10-06

Case description:
Have the following code (posted below), I this machine VS debug and IIS erection test can receive data (native IIS7.5), but the POST request is sent to the server or receiving data, the service side iis6.0, using WebClient service side with the test data can be received

Console main code:
 
Private static async Task SendRequestEx (url string, int eventIndex, string carNum, long sn, int recordType, string doorNum,
Int inOrOut, int valid, string eventDate, string des, string photoName, string Token)
{
//HttpClient client=new HttpClient {BaseAddress=new Uri (url)};
Using (HttpClient client=new HttpClient ())
{
//HttpClient client=new HttpClient ();
Client. MaxResponseContentBufferSize=256000;
Client. DefaultRequestHeaders. Add (" the user-agent, "
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36 ");
Using (MultipartFormDataContent content=new MultipartFormDataContent (DateTime. Now. The Ticks. ToString (" X ")))
{
Content. The Add (new StringContent (Convert. ToString (eventIndex)), "event_index");
Content. The Add (new StringContent (carNum), "card_num");
Content. The Add (new StringContent (Convert. ToString (sn)), "machine_sn");
Content. The Add (new StringContent (Convert. ToString (recordType)), "record_type");
Content. The Add (new StringContent (doorNum), "door_num");
Content. The Add (new StringContent (Convert. ToString (inOrOut)), "in_or_out");
Content. The Add (new StringContent (Convert. ToString (valid)), "is_valid");
Content. The Add (new StringContent (Convert. ToString (eventDate)), "event_date");
Content. The Add (new StringContent (Convert. ToString (des)), "event_des");
Content. The Add (new StringContent (Convert. ToString (photoName)), "photo");
Content. The Add (new StringContent (Convert. ToString (Token)), "Token");
Var response=await client. PostAsync (url, content);
Return the response. The content. ReadAsStringAsync (). The Result;
}
}
}


The server receives the code is as follows:
 
String eventIndex=context. Request [" event_index "];
String cardNum=context. Request [" card_num "];
String machineSn=context. Request [" machine_sn "];
String recordType=context. Request [" record_type "];
String doorNum=context. Request [" door_num "];
String inOrOut=context. Request [" in_or_out "];
String isValid=context. Request [" is_valid "];
String eventDate=context. The Request (" event_date ");
String eventDes=context. Request [" event_des "];
String token=context. The Request (" token ");
String photo=null;

Javis. WriteLog (" the Form - Data: [event_index] : "+ eventIndex +";" +
"[card_num] :" + cardNum + ";" +
"[machine_sn] :" + machineSn + ";" +
"[record_type] :" + recordType + ";" +
"[door_num] :" + doorNum + ";" +
"[in_or_out] :" + inOrOut + ";" +
"[is_valid] :" + isValid + ";" +
"[event_date] :" + eventDate + ";" +
"[event_des] :" + eventDes + ";" +
"[token] :" + token + ";" );

If (string. IsNullOrEmpty (eventIndex) | |
String. IsNullOrEmpty (cardNum) | |
String. IsNullOrEmpty (machineSn) | |
String. IsNullOrEmpty (recordType) | |
String. IsNullOrEmpty (doorNum) | |
String. IsNullOrEmpty (inOrOut) | |
String. IsNullOrEmpty isValid () | |
String. IsNullOrEmpty (eventDate) | |
String. IsNullOrEmpty (eventDes) | |
String. IsNullOrEmpty (token))
{
The context. The Response. The Write (" data "illegal);
return;
}
//here database insert


Print log has been empty, I don't know for what reason, trouble, can you help me to look at

CodePudding user response:

Try ~ in another way

CodePudding user response:

reference 1/f, 711 glaciers response:
try another way ~

Can be the other way, because this is a more concise code style, and I just want to know where is the reason, otherwise meet next time still have to write a relatively lengthy code, I want to a more refined further trouble for help

CodePudding user response:

Async is your external, internal is. The Result, you will certainly be a deadlock so very not...
Then HttpClient don't release, normal should be static, creating frequently leads to run out of port, static, of course, also can have not update the DNS

CodePudding user response:

she would play the soy sauce reference 3 floor response:
you outside is async, is internal. The Result, you will certainly be a deadlock so very not...
Then HttpClient don't release, normal should be static, creating frequently leads to run out of port, such as static, of course, also can have the DNS update problem

Thank you bosses give advice or comments, but I can't the receiving data of the problem is what problem?

CodePudding user response:

Not receive any data, is the final Result leads to program the deadlock
You will response. Content. ReadAsStringAsync (). The Result to await the response. The content. ReadAsStringAsync () and have a look

CodePudding user response:

reference 5 floor Eva can play soy sauce response:
is not receive any data, is the final Result leads to program the deadlock
You will be the response. The content. ReadAsStringAsync (). The Result to await the response. The content. ReadAsStringAsync () to see

Is still the same result, I've just tried, because I used to use fiddler caught is to be able to send a POST request, the server can receive, but no data, I don't know why
  •  Tags:  
  • C#
  • Related