0

I built a C# application about 8 months ago to pull SurveyMonkey responses and store them on our SQL Server. The application has run every day for over 6 months without any issue, but suddenly Friday morning I can't get any requests to go through. I'm not seeing anything on the developer site mentioning outages, so I've been examining my code for possible issues.

This is the first app I've ever written to create web requests, so it's possible I'm doing things badly.

Request, from Fiddler:

POST https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=<key hidden> HTTP/1.1
Authorization: bearer <token hidden>
Content-Type: application/json
Host: api.surveymonkey.net
Content-Length: 146
Expect: 100-continue
Connection: Keep-Alive

{
"page": 1,
"fields": ["title","analysis_url","preview_url","date_created","date_modified","language_id","question_count","num_responses"]
}

Response body, from Fiddler:

{"status":1,"errmsg":"Request header \"Authorization\" token not found"}

C# code:

private JObject SubmitPostRequest(string URIPath, string RequestBody)
{
    using (var webClient = new WebClient())
    {
        // Create URI for POST request to go to. URI varies depending on API method and includes API Key.
        Uri requestURI = new Uri(APIHost, URIPath + "?api_key=" + APIKey);

        // Have web client use NT credentials for proxy server
        webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

        // Specify headers: access token in authorization header, content type
        webClient.Headers["Authorization"] = "bearer " + AccessToken;
        webClient.Headers["Content-Type"] = "application/json";

        // Create requestBody as byte[]
        byte[] requestBody = Encoding.Default.GetBytes(RequestBody);

        // Connect to the URI and send requestBody as a POST command.
        byte[] postResponse = webClient.UploadData(requestURI, "POST", requestBody);

        // Update LastConnect
        LastConnect = DateTime.Now;

        ++TransactionCounter;

        // Convert byte[] response to string, parse string and return as JObject.
        JObject jsonResponse = JObject.Parse(Encoding.Default.GetString(postResponse));

        // Evaluate "status" field of jsonResponse. Throw exception if response is not 0.
        dynamic dyn = jsonResponse;
        if (dyn.status != 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("HTTP Post request failed:");
            sb.Append("\tstatus: ").AppendLine(dyn.status.ToString());
            sb.Append("\terrmsg: ").AppendLine(dyn.errmsg.ToString());
            throw new WebException(sb.ToString());
        }

        return jsonResponse;
    }
}
4

1 回答 1

0

这花了一些时间,但我设法与公司 IT 部门的某个人取得了联系,他负责监控流量以了解发生了什么。他们对代理服务器进行了修复,一切终于又恢复了。

于 2014-12-21T17:40:39.310 回答