我的双手在空中 - 我哭了,叔叔。帮助我理解:
我给客户写了一个简单的HttpWebRequest帖子' https://website。 它具有特定的头要求和JSON请求体。收到GetResponse StatusCode.OK后,GetResponseStream是由其服务器提供的错误。他们检查了他们的日志,并在标题出现时说,请求Body是"。
这是我的代码:
public const string API_URILogin = "https://dev.clientURL.com/test/api/login";
private void RequestTest()
{
LoginAPIStruct loginInfo = new LoginAPIStruct();
loginInfo.loginId = TestId;
loginInfo.password = TestPw;
loginInfo.vehicleType = "E";
loginInfo.deviceId = "ABCDEF";
loginInfo.clientId = "123456";
loginInfo.appVersion = string.Empty;
loginInfo.osType = string.Empty;
loginInfo.osVersion = string.Empty;
// Call LoginAPI
string loginStatus = LoginAPI(loginInfo);
//Display Results
lblResults.Text = loginStatus;
}
// Public Variables
public static string errorMessage;
// ** Client Login **
public static string LoginAPI(LoginAPIStruct loginInfo)
{
// Post Login to Client. Get response with Vehicle structure.
string loginInfoSerialized = string.Empty;
string returnString = string.Empty;
loginInfoSerialized = new JavaScriptSerializer().Serialize(loginInfo);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(API_URILogin);
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("From", "CC");
request.Headers.Add("Language", "0");
request.Headers.Add("Offset", "-8");
request.Headers.Add("To", "CCM");
// Post Request
using (StreamWriter streamPost = new StreamWriter(request.GetRequestStream()))
{
streamPost.Write(loginInfoSerialized);
}
// Get Response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// Status Okay.
//string responseStatusDescription = response.StatusDescription;
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
try
{
var result = streamReader.ReadToEnd();
returnString = result;
}
catch (Exception ex)
{
returnString = ex.Message;
}
}
}
else
{
returnString = response.StatusCode.ToString();
}
return returnString;
} // end LoginAPI
我的JSON字符串似乎有效 - 我将其粘贴到Fiddler中,同时设置标题,Fiddler返回成功登录。
从ASP.Net,它返回所记录的错误,即需要电子邮件地址(loginId)。我的C#代码中没有看到任何不正确的内容。还有什么想法可以看吗?客户端是否可以在其托管网站上阻止或过滤掉某个请求?我被卡住了。
答案 0 :(得分:0)
所以,对于任何想要知道的人......你必须"关闭"执行.Write(payload)命令后的StreamWriter,否则它实际上并不发送数据。
// Post Request
StreamWriter streamPost = new StreamWriter(request.GetRequestStream());
streamPost.Write(loginInfoSerialized);
streamPost.Close(); //Don't forget to Close() or no data sent!!
我希望这可以帮助别人,因为我花了两周的时间来解决这个问题。侨
答案 1 :(得分:0)
我会像这样发帖:
<script src="myscripts.js"></script>