C# 将json发布到远程服务器(REST)
我想使用此代码将一些json数据发送到远程服务器(Rest,在我的控制之外),发送方式如下: 我首先创建url和请求方法:C# 将json发布到远程服务器(REST),c#,json,web-services,rest,internal-server-error,C#,Json,Web Services,Rest,Internal Server Error,我想使用此代码将一些json数据发送到远程服务器(Rest,在我的控制之外),发送方式如下: 我首先创建url和请求方法: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gUrlDot); request.Method = "POST"; Dictionary<String, String> lDictionary = new Dictionary<Strin
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(gUrlDot);
request.Method = "POST";
Dictionary<String, String> lDictionary = new Dictionary<String, String>();
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
Dot lDot= new Dot();
serviceContext lserviceContext = new serviceContext();
items litems = new items();
lDot.rad = pClient;
lDictionary.Add("companyId", "00230");
lDictionary.Add("treatmentDate", lGlobals.FormatDateYYYYMMDD());
lDictionary.Add("country", "FR");
lDictionary.Add("language", "fr");
lDictionary.Add("Id", "test");
litems.contextItem = lDictionary;
lDot.serviceContext = lserviceContext;
lDot.serviceContext.items = litems;
String Input=_Tools.JsonSerializer(lDot);
log.Debug("Input Dot " + Input);
Byte[] byteArray = encoding.GetBytes(Input);
request.ContentLength = byteArray.Length;
request.ContentType = @"application/json";
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
}
long length = 0;
我在这里遗漏了什么?一个500错误意味着您向其发出请求的服务器上有问题。你需要检查以确保两件事 1) 确保您的请求格式正确,并且请求的资源没有任何意外或无效值 2) 您想与之交谈的服务器可以获得请求,并且是最新的(如果您控制客户端)
在这两种情况下,500最常见的原因是客户端服务器出现了超出您控制范围的错误。您是否收到来自服务器的错误消息(如使用fiddler调试)?Http状态代码“500”表示内部服务器错误,如果您的请求有效,则错误可能来自远程服务器。
try
{
using (var response = (HttpWebResponse)request.GetResponse())
{
length = response.ContentLength;
string output = response.ToString();
lTrace.AppendLine("-Flux Json recu => " + response.StatusCode + " " + length);
log.Debug("Output Dot " + output);
}
log.Info(LogsHelper.LogHeader("End processing Get list ", pClient, Service.SrvGetList, "", lResponse.StatusCode, lResponse.StatusLabel, lResponse.ResponseObject, ref lTrace));
}
catch (Exception ex)
{
lResponse.StatusCode = StatusCodes.ERROR_COMMUNICATION;
log.Error(LogsHelper.LogHeader("End processing Get list", pClient, Service.SrvGetList, "", lResponse.StatusCode, ex.Message, lResponse.ResponseObject, ref lTrace));
}
return lResponse;
}