Skip to main content
 首页 » 编程设计

c#之HttpWebRequest POST 给出 500 服务器错误

2024年02月24日36jyk

我需要从我的应用程序中创建一个经过身份验证的 httpwebrequest。对我的请求的响应应该是 json 格式。为此,我使用以下代码:

// Create the web request  
        Uri address = new Uri("http://www.mysite.com/remote/user/login/format/json"); 
        HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; 
        request.Method = "POST"; 
        request.UseDefaultCredentials = false; 
        request.Credentials = new NetworkCredential(UserName, Password); 
        request.PreAuthenticate = true; 
        request.ContentType = "application/x-www-form-urlencoded"; 
        request.Accept = "application/json"; 
 
        string data = string.Format("username={0}&password={1}", otherusername, otherpassword); 
        // Create a byte array of the data we want to send   
        byte[] byteData = UTF8Encoding.UTF8.GetBytes(data); 
 
        // Set the content length in the request headers   
        request.ContentLength = byteData.Length; 
 
         //Write data   
        using (Stream postStream = request.GetRequestStream()) 
        { 
            postStream.Write(byteData, 0, byteData.Length); 
        } 
 
 
        // Get response 
        try 
        { 
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
            { 
                // Get the response stream   
                StreamReader reader = new StreamReader(response.GetResponseStream()); 
 
                // Console application output   
                jsonResponse = reader.ReadToEnd(); 
                reader.Close(); 
            } 
 
            user = new User(); 
            JObject o = JObject.Parse(jsonResponse); 
            user.Unguessable_id = (string)o["unguessable_id"]; 
            user.Print_id = (string)o["print_id"]; 
            user.Rrid = (string)o["rrid"]; 
            user.Raid = (string)o["raid"]; 
 
        } 
        catch (WebException ex) { 
            errorMessage = ex.Message; 
        } 

问题是第一次调用它总是在服务器上给出 500 错误。并且请求失败。如果我重做调用(通过在我的浏览器中进行刷新)请求成功。

请求在正常情况下应该是这样的:

POST /remote/user/login/format/json HTTP/1.1 
Host: <yourhost> 
 
username=user&password=pass 

但是当服务器发出 500 错误时,他收到了这样的消息:

username=user&password=passwordPOST /remote/user/login/format/json HTTP/1.1 

知道为什么会这样吗?在我的测试应用程序中,如果我刷新使 httpwebrequest 调用成功的页面。

编辑: 安装 Fiddler 后,发出的请求如下所示:

=> 产生 500 的那个

POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Accept: application/json 
Host: www.mysite.com 
Content-Length: 30 
Expect: 100-continue 
Connection: Keep-Alive 
 
username=user&password=pass 

=> 刷新时生成的

POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Accept: application/json 
Authorization: Basic ZGNpOkFpR2g3YWVj 
Host: www.mysite.com 
Content-Length: 30 
Expect: 100-continue 
Connection: Keep-Alive 
 
username=user&password=pass 

似乎 Authorization: Basic ZGNpOkFpR2g3YWVj 没有包含在第一个请求中......为什么会这样?(我对两个请求使用相同的代码)

请您参考如下方法:

我建议您安装 Fiddler看看到底发生了什么