protected void Button1_Click(object sender, EventArgs e)
{
MemoryStream stream = new MemoryStream();
StreamReader reader = new StreamReader(stream);
GetReportRequest request = new GetReportRequest();
request.ReportId = "24537536063";
request.Merchant = sellerId;
request.MWSAuthToken = mwsAuthToken;
request.Report = stream;
ltMsg.Text = InvokeGetReport(request);
string text = reader.ReadToEnd();
stream.Dispose();
stream.Close();
reader.Dispose();
reader.Close();
DataTable dt = ConvertReport2DT(text);
GridView1.DataSource = dt;
GridView1.DataBind();
//Response.Write(text);
}
public static string InvokeGetReport(GetReportRequest request)
{
StringBuilder builder = new StringBuilder();
try
{
MarketplaceWebService.MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
config.ServiceURL = serviceURL;
MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient
(accessKey, secretKey, appName, appVersion, config);
GetReportResponse response = service.GetReport(request);
builder.AppendFormat("Service Response");
builder.AppendFormat("=============================================================================");
builder.AppendFormat(" GetReportResponse");
if (response.IsSetGetReportResult())
{
builder.AppendFormat(" GetReportResult");
GetReportResult getReportResult = response.GetReportResult;
if (getReportResult.IsSetContentMD5())
{
builder.AppendFormat(" ContentMD5");
builder.AppendFormat(" {0}", getReportResult.ContentMD5);
}
}
if (response.IsSetResponseMetadata())
{
builder.AppendFormat(" ResponseMetadata");
ResponseMetadata responseMetadata = response.ResponseMetadata;
if (responseMetadata.IsSetRequestId())
{
builder.AppendFormat(" RequestId");
builder.AppendFormat(" {0}", responseMetadata.RequestId);
}
}
builder.AppendFormat(" ResponseHeaderMetadata");
builder.AppendFormat(" RequestId");
builder.AppendFormat(" " + response.ResponseHeaderMetadata.RequestId);
builder.AppendFormat(" ResponseContext");
builder.AppendFormat(" " + response.ResponseHeaderMetadata.ResponseContext);
builder.AppendFormat(" Timestamp");
builder.AppendFormat(" " + response.ResponseHeaderMetadata.Timestamp);
}
catch (MarketplaceWebServiceException ex)
{
builder.AppendFormat("Caught Exception: " + ex.Message);
builder.AppendFormat("Response Status Code: " + ex.StatusCode);
builder.AppendFormat("Error Code: " + ex.ErrorCode);
builder.AppendFormat("Error Type: " + ex.ErrorType);
builder.AppendFormat("Request ID: " + ex.RequestId);
builder.AppendFormat("XML: " + ex.XML);
builder.AppendFormat("ResponseHeaderMetadata: " + ex.ResponseHeaderMetadata);
}
return builder.ToString();
}
#region 报告字符串转换成DataTable
/// <summary>
/// 报告字符串转换成DataTable
/// </summary>
/// <param name="reportMsg">报告字符串</param>
/// <returns></returns>
public static DataTable ConvertReport2DT(string reportMsg)
{
DataTable dt = new DataTable();
try
{
//\t \r\n
if (reportMsg.Contains("\r\n"))
{
string[] rows = reportMsg.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
//第一行表头
if (rows[0].Contains("\t")) //制表符
{
string[] columns = rows[0].Split('\t');
for (int i = 0; i < columns.Length; i++)
{
if (!dt.Columns.Contains(columns[i]))
{
dt.Columns.Add(columns[i]);
}
}
}
//第二行开始数据
for (int i = 1; i < rows.Length; i++)
{
string[] datas = rows[i].Split('\t');
DataRow dr = dt.NewRow();
for (int j = 0; j < datas.Length; j++)
{
dr[j] = datas[j];
}
dt.Rows.Add(dr);
dr = null;
}
}
}
catch { }
return dt;
}
#endregion
获取报告 Stream转string,利用字符串分割转换成DataTable
本文转载:CSDN博客