C#HttpWebResponse上传PDF不适用于word文档

时间:2018-05-17 20:40:06

标签: c# pdf multipart httpwebresponse system.net.httpwebrequest

我试图发布一个pdf文件来休息api,我得到一个500内部服务器错误。该代码适用于单词doc,它也可以在Visual Studio 2015中以调试模式运行,但在生产中失败。此时我不确定它是否是代码问题。帮助,没有多少头发留下。

我试过IE和Chrome都没有区别。我试过其他文档文件类型确实很好,但PDF。除了内容类型之外,我是否必须为PDF做一些不同的事情?

private bool Request_login_mydgsi_ca(out HttpWebResponse response)
{
    response = null;

    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://login.mydgsi.ca/WebAPI/Attachment?aboutType=Candidate&referenceID="+Candidate_id.Trim()+"&attachmentTypeID=Resume&Name=Resume&expirationDate=1900/01/01&Note=Candidate%20Resume");

        request.Headers.Set(HttpRequestHeader.CacheControl, "no-cache");

        string client_id = ConfigurationManager.AppSettings["client_id"].ToString();
        string secret_id = ConfigurationManager.AppSettings["secret_id"].ToString();

        ERApplyForJobs ePosition = new ERApplyForJobs(client_id, secret_id, "");
        string tokenAPI = ConfigurationManager.AppSettings["APIToken"].ToString();
        string secruityToken = ePosition.getSecurityToken(tokenAPI);
        request.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + secruityToken );

        request.ContentType = "multipart/form-data; boundary=---224842443399224314204538";
        request.KeepAlive = true;
        request.Method = "POST";
        request.ServicePoint.Expect100Continue = true;
        string contentType = MimeMapping.GetMimeMapping(fileName);
        string body = @"---224842443399224314204538
            Content-Disposition: form-data; name=""""; filename=""" + fileName + @"""
            Content-Type: " + contentType + @"<!>" + fullFileName + @"<!>---224842443399224314204538--";


        WriteMultipartBodyToRequest(request, body);
        response = (HttpWebResponse)request.GetResponse();


    }
    catch (WebException e)
    {
        response = (HttpWebResponse)e.Response;

    }
    catch (Exception)
    {
        if (response != null) response.Close();
            return false;
    }

    return true;
}

1 个答案:

答案 0 :(得分:0)

该问题与文件上传有关,无法获取完整路径。奇怪的是它在单词中起作用,但对PDF不起作用。

我不确定为什么它适用于word文档而且无法用于PDF。按照我的方式,我认为两者都应该失败。