2

我正在与 eBay 大型商家服务部合作。我已经成功地使用 .NET HttpClient(最后)获得了基本的 AddFixedPriceItem 工作。但是,根据我的 AddFixedPriceItem XML 文件的内容,uploadFile 作业的响应将返回:

请指定具有有效格式的文件

我无法确定导致此错误的原因。

示例 #1:

我有一个包含 2 个AddFixedPriceItemRequest请求的文件,它会成功上升,而一个包含 3 个AddFixedPriceItemRequest请求的文件不会。当我NameValueList从第三个请求中删除一些标签时,它会起作用。我没有看到NameValueList我删除的那些标签有任何问题或不同。如果我自己发送第三个请求,而不删除任何标签,它就可以完美地工作。

示例 #2:

AddFixedPriceItemRequest在一个包含标签的 CDATA 信息Description(用于 HTML 值)的文件中有 1 个请求,该文件将成功上升,另一个包含 2 个AddFixedPriceItemRequest请求的文件也包含几乎相同的 CDATA 值但不起作用。如果我从添加的第二个请求中删除 CDATA,它仍然不起作用。如果我也从第一个请求中删除了 CDATA(第一次自行成功上升),那么它确实有效。

我真的很困惑。关于哪个文件被认为是“有效的”似乎没有押韵和理由。

下面是一个失败的请求示例:(Stack Overflow 似乎没有采用 Content-ID - 但它们就在那里。)

POST http://storage.sandbox.ebay.com/FileTransferService HTTP/1.1
X-EBAY-SOA-SECURITY-TOKEN: xxx
X-EBAY-SOA-SERVICE-NAME: FileTransferService
X-EBAY-SOA-SERVICE-VERSION: 1.0.0
X-EBAY-SOA-OPERATION-NAME: uploadFile
X-EBAY-SOA-OPERATION-FORMAT: XML
Content-Type: multipart/related; boundary="MIME_boundary"; type="application/xop+xml"; start="<0.urn:uuid:9ce221c2-659d-4852-a166-51dcbccf68d9>"; start-info="text/xml"
Host: storage.sandbox.ebay.com
Content-Length: 3041
Expect: 100-continue

--MIME_boundary
Content-Type: application/xop+xml
Content-ID: <0.urn:uuid:9ce221c2-659d-4852-a166-51dcbccf68d9>
Content-Transfer-Encoding: binary

<?xml version="1.0" encoding="utf-8"?><uploadFileRequest xmlns="http://www.ebay.com/marketplace/services"><fileAttachment><Data><xop:Include href="cid:urn:uuid:c6f7fc4d-352b-414f-8638-6c9bdf40519e"  xmlns:xop="http://www.w3.org/2004/08/xop/include"/></Data><Size>1710</Size></fileAttachment><fileFormat>zip</fileFormat><fileReferenceId>50008675134</fileReferenceId><taskReferenceId>50008489744</taskReferenceId></uploadFileRequest>
--MIME_boundary
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-ID: <urn:uuid:c6f7fc4d-352b-414f-8638-6c9bdf40519e>

UEsDBBQAAAAIAMZZ9UhnranV5gUAADYmAAAPACQAYWRkbGlzdGluZ3MueG1sCgAgAAAAAAABABgA4roeimLj0QE1N5fd4t3RAex2PDlG3dEB7Vptc9o4EP4rmny59kOxDZiXjOoZXppL5kjDxaG5+9QR9gJqbcsny2m4X38rgwEbmJ57zDWd8inSrvaRdq1dPRKh/TT4PGSKvXv2Fiyawz38lUKiEvIcBlHy9iKV0SVM2fKSxTy5hP6ozxIYiDAWEUQquXDoNTAfpEM/gEy4iJxuy6ZG3qEuV3AzdExqrFvUyA16vn/Fn8EfS+7BjYJwPfe/n/qdlEKOcNkpm4MD0ceJS42ikD4yGfFoPoInCJxrPl9QoyA6sm69HocOmIK5kMtbFsdo0QsC8QV8R8kUqHFESQci8rlCFPTWMk10fVeC6jRScunoteZtOkilhMjT0iGK8x4dQuJJHmtbZ8h4JAKMIhmhGwqnJMPRda9lN6/e3PX7xP2Cojc9GRJrNYJcBUJIMmJhTHhE7nhA+jJNFuDjXxH9jT7s4tMhT2KmvMUDD+GWPTsYj7KI3kRPGHz0+kEy7zNOeAtqIXzH/W1CjWNKOhIey+YwO23Tosamn8XZjcHjM+4lDn3PQvjAghRGPFGrrtOXLPKpkbVpptyGAr9YJlipS5ZloNvx+xLMTviqIQkfghNhYQZOycMyhiLeNQvEHKLKUP8dZQBS6c+RfaEi3MDtEYNMRpXwrnjEk0UR6NBerBJ/9kwemVKY4UVczLcvlZDep+EUJBEzooOXlNAqQd09gWRBQIaYK5GuIiU0u0luohq5Jn8Qq5M1H3XTzJrHQ2qUUkR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CTO6FCA/UqLsZlolqWxJZFx6lB7BWikpYu1E7yc7Sq9L7age4CHjL5hEo7lVC/SCCA28LdfOpEsoRBx+rgXBfLXAzDDn2VNm7zvHK8J3fDXJS8WM8HGwp0Et5OcAV1a3zb1znl4Of+OWg2dh/Oai36xVeDr7pmrn3I5fZsVseXortrj1tWp1Wt9Xw/DbzZzbmadOr8COXcex/w5x/AFBLAQItABQAAAAIAMZZ9UhnranV5gUAADYmAAAPACQAAAAAAAAAIAAAAAAAAABhZGRsaXN0aW5ncy54bWwKACAAAAAAAAEAGADiuh6KYuPRATU3l93i3dEB7HY8OUbd0QFQSwUGAAAAAAEAAQBhAAAANwYAAAAA
--MIME_boundary--

这是我发送请求的方式HttpClient

    byte[] data;
long fileLength;
var uuidRequest = Guid.NewGuid().ToString();
var uuidAttachment = Guid.NewGuid().ToString();

using (FileStream fs = File.OpenRead(filePath))
{
    data = new byte[fs.Length];
    fs.Read(data, 0, data.Length);
    fileLength = fs.Length;
}

var xml =
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
    "<uploadFileRequest xmlns=\"http://www.ebay.com/marketplace/services\">" +
        "<fileAttachment>" +
            "<Data><xop:Include href=\"cid:urn:uuid:" + uuidAttachment + "\"  xmlns:xop=\"http://www.w3.org/2004/08/xop/include\"/></Data>" +
            "<Size>" + fileLength + "</Size>" +
        "</fileAttachment>" +
        "<fileFormat>zip</fileFormat>" +
        "<fileReferenceId>" + fileReferenceID + "</fileReferenceId>" +
        "<taskReferenceId>" + jobID + "</taskReferenceId>" +
    "</uploadFileRequest>";

var multippartcontent = new MultipartContent("related", "MIME_boundary");
multippartcontent.Headers.Remove("Content-Type");
multippartcontent.Headers.TryAddWithoutValidation("Content-Type", string.Format("multipart/related; boundary=\"MIME_boundary\"; type=\"application/xop+xml\";start=\"<0.urn:uuid:{0}>\";start-info=\"text/xml\"", uuidRequest));

var content = new StringContent(xml);
content.Headers.ContentType = new MediaTypeHeaderValue("application/xop+xml");
content.Headers.Add("Content-ID", string.Format("<0.urn:uuid:{0}>", uuidRequest));
content.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "binary");
multippartcontent.Add(content);

var attachmentContent = new StringContent(Convert.ToBase64String(data));
attachmentContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
attachmentContent.Headers.TryAddWithoutValidation("Content-Transfer-Encoding", "base64");
attachmentContent.Headers.Add("Content-ID", string.Format("<urn:uuid:{0}>", uuidAttachment));
multippartcontent.Add(attachmentContent);


var uploadFileResp = httpclient.PostAsync(ftServiceUrl, multippartcontent).Result.Content.ReadAsStringAsync().Result;

我会注意到,当我从 eBay ( https://ebaydts.com/eBayKBDetails?KBid=1338 )上传这个对我不起作用的 LMS 项目的相同文件时。这让我觉得我在代码中做错了什么——也许与我的编码方式有关?

HTTP 响应

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
X-EBAY-SOA-SERVICE-METRICS: 455075973
X-EBAY-SOA-REQUEST-ID: 15613449-f630-a471-d2d4-ec64ffff71c7!FileTransferService!10.71.29.45!v3apibulksandboxcore[]
X-EBAY-SOA-SERVICE-VERSION: 1.1.0
X-EBAY-SOA-MESSAGE-PROTOCOL: NONE
X-EBAY-SOA-RESPONSE-DATA-FORMAT: XML
X-EBAY-SOA-OPERATION-NAME: uploadFile
X-EBAY-SOA-SERVICE-NAME: {http://www.ebay.com/marketplace/services}FileTransferService
Content-Type: text/xml;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 22 Jul 2016 15:41:19 GMT

1cd
<?xml version='1.0' encoding='UTF-8'?><uploadFileResponse xmlns="http://www.ebay.com/marketplace/services"><ack>Failure</ack><errorMessage><error><errorId>11</errorId><domain>Marketplace</domain><severity>Error</severity><category>Application</category><message>Please specify a File with Valid Format</message><subdomain>FileTransfer</subdomain></error></errorMessage><version>1.1.0</version><timestamp>2016-07-22T15:41:20.118Z</timestamp></uploadFileResponse>
0
4

1 回答 1

0

我终于得到了答案。“100 continue”消息给了我所需的线索。我花了太多时间试图了解间歇性操作。

1)您的代码永远不会真正起作用。当您运行 LMS 项目时,会建立一个 cookie,它会为您的请求设置默认设置。因此,您的代码在运行 LMS 后将始终有效。如果您从 IE 历史记录中删除 cookie,您的代码将永远无法工作。

2)“100 continue message”用于“Chunk Mode”(不是流)。Net 库默认为 HTTP 1.1,而现在大多数服务器的 see 1.1 将默认为块模式。服务器发送一个块,但网络库中没有办法从服务器请求第二个块。所以连接超时并失败。解决此问题的唯一方法是将 HTTP 请求设置为不支持块模式的 HTTP 1.0,服务器会将响应作为流发送回。

尝试这个

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL");
            request.ProtocolVersion = HttpVersion.Version10;

另请参阅:在 HttpClient 中设置 HTTP 协议版本

查看失败的第一行请求,它有 1.1

于 2016-07-21T19:21:30.927 回答