eBay大中华区API开发者平台 开发者专区

搜 索
首页>API开发者平台>技术文档>基于.NET SDK的C#例程:UploadSiteHostedPictures

技术文档

问题
基于.NET SDK的C#例程:UploadSiteHostedPictures
解答
0
人觉得答案有帮助)

详细描述

此C#例程演示了如何调用UploadSiteHostedPictures向eBay的Picture Servers上载图片。
 

using System; 
using System.IO; 
using System.Text; 
using System.Xml; 
using System.Net;

namespace Samples 
{

public class Samples 
{
public void UploadPictures(string imagefile) 

 

            //read the image file as a byte array

            System.IO.FileStream fs = new System.IO.FileStream(imagefile, FileMode.Open,FileAccess.Read); 
            fs.Seek(0, SeekOrigin.Begin); 
            System.IO.BinaryReader br = new System.IO.BinaryReader(fs);

            byte[] image = br.ReadBytes((int)fs.Length);

            br.Close();

            fs.Close();

 

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.ebay.com/ws/api.dll");

            HttpWebResponse resp = null;

 

            string boundary = "MIME_boundary";

            string CRLF = " ";

           

            //Add the request headers

            req.Headers.Add("X-EBAY-API-COMPATIBILITY-LEVEL", "515");

            req.Headers.Add("X-EBAY-API-DEV-NAME", "devid");             //use your devid

            req.Headers.Add("X-EBAY-API-APP-NAME", "appid");             //use your appid

            req.Headers.Add("X-EBAY-API-CERT-NAME", "certid");          //use your certid

            req.Headers.Add("X-EBAY-API-SITEID", "0");

            req.Headers.Add("X-EBAY-API-DETAIL-LEVEL", "0");

            req.Headers.Add("X-EBAY-API-CALL-NAME", "UploadSiteHostedPictures");

            req.ContentType = "multipart/form-data; boundary=" + boundary;

       

            //set the method to POST

            req.Method = "POST";

            //set the HTTP version to 1.0

            req.ProtocolVersion = HttpVersion.Version10;

 

            //replace token with your own token

            string token = "token";


            //Construct the request 
           string strReq1 = "--" + boundary + CRLF

                            + "Content-Disposition: form-data; name=document" + CRLF

                            + "Content-Type: text/xml; charset="UTF-8"" + CRLF + CRLF

                            + "<?xml version="1.0" encoding="utf-8"?>"

                            + "<UploadSiteHostedPicturesRequest xmlns="urn:ebay:apis:eBLBaseComponents">"

                            + "<RequesterCredentials>"

                            + "<eBayAuthToken>" + token + "</eBayAuthToken>"

                            + "</RequesterCredentials>"

                            + "</UploadSiteHostedPicturesRequest>"

                            + CRLF + "--" + boundary + CRLF

                            + "Content-Disposition: form-data; name=image; filename=image" + CRLF

                            + "Content-Type: application/octet-stream" + CRLF

                            + "Content-Transfer-Encoding: binary" + CRLF + CRLF; 
 

           string strReq2 = CRLF + "--" + boundary + "--" + CRLF;

 

            //Convert the string to a byte array 
            byte[] postDataBytes1 = System.Text.Encoding.ASCII.GetBytes(strReq1); 
            byte[] postDataBytes2 = System.Text.Encoding.ASCII.GetBytes(strReq2); 

            int len = postDataBytes1.Length + postDataBytes2.Length + image.Length; 
            req.ContentLength = len; 
 

            //Post the request to eBay 
            System.IO.Stream requestStream = req.GetRequestStream(); 
            requestStream.Write(postDataBytes1, 0, postDataBytes1.Length); 
            requestStream.Write(image, 0, image.Length); 
            requestStream.Write(postDataBytes2, 0, postDataBytes2.Length); 
            requestStream.Close();

 

            string response;

            try

            {

                // get response and write to console

                resp = (HttpWebResponse)req.GetResponse();

                StreamReader responseReader = new StreamReader(resp.GetResponseStream(),Encoding.UTF8);

                response = responseReader.ReadToEnd();

                resp.Close();

 

                //response contains our pictures url

                System.Xml.XmlDocument xml = new System.Xml.XmlDocument();

                xml.LoadXml(response);

 

                //Extract the FullURL from the response

                System.Xml.XmlNodeList list = xml.GetElementsByTagName("FullURL","urn:ebay:apis:eBLBaseComponents");

                string fullURL = list[0].InnerText;

 

                //Get the other elements from the response if required

            }

            catch (Exception ex)

            {

                //handle exception

                response = ex.Message;

            }

    }

}

}

 


你会获取如下响应:
 

<?xml version="1.0" encoding="utf-8"?>

<UploadSiteHostedPicturesResponse xmlns="urn:ebay:apis:eBLBaseComponents">

  <Timestamp>2007-06-22T23:28:57.929Z</Timestamp>

  <Ack>Success</Ack>

  <Version>517</Version>

  <Build>e517_core_Bundled_4784308_R1</Build>

  <PictureSystemVersion>2</PictureSystemVersion>

  <SiteHostedPictureDetails>

    <PictureSet>Standard</PictureSet>

    <PictureFormat>JPG</PictureFormat>

    <FullURL>http://i8.ebayimg.com/02/i/000/a6/82/ec8e_1.JPG?set_id=7</FullURL>

    <BaseURL>http://i8.ebayimg.com/02/i/000/a6/82/ec8e_</BaseURL>

    <PictureSetMember>

      <MemberURL>http://i8.ebayimg.com/02/i/000/a6/82/ec8e_0.JPG?set_id=7</MemberURL>

      <PictureHeight>16</PictureHeight>

      <PictureWidth>16</PictureWidth>

    </PictureSetMember>

    <PictureSetMember>

      <MemberURL>http://i8.ebayimg.com/02/i/000/a6/82/ec8e_1.JPG?set_id=7</MemberURL>

      <PictureHeight>16</PictureHeight>

      <PictureWidth>16</PictureWidth>

    </PictureSetMember>

    <PictureSetMember>

      <MemberURL>http://i8.ebayimg.com/02/i/000/a6/82/ec8e_2.JPG?set_id=7</MemberURL>

      <PictureHeight>16</PictureHeight>

      <PictureWidth>16</PictureWidth>

    </PictureSetMember>

  </SiteHostedPictureDetails>

</UploadSiteHostedPicturesResponse>


 

 

 


版本信息

上面例程基于此特定的API版本:

API Schema 版本 515


附件
 

 


答案对您有帮助吗?

是,对我很有帮助
否,没解决我的问题