ASPNETnewbie
Programmer
This program is supposed to collect at the needed data from my ASPX Form and transmit the information to the USPS API which in turned returns the information for printing out a Label but it seems not to work and I would appreciate it if someone can help please...
ASPNETnewbie
ASPNETnewbie
Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
public partial class Controls_GetLabel : System.Web.UI.UserControl
{
// put together the XML to transmit so you can get your LABEL
private string BuiltXML;
private string user_id = " ";
private string theURL = "[URL unfurl="true"]http://servername/shippingAPITest.dll?API=";[/URL]
protected void Page_Load(object sender, EventArgs e)
{
}
private void buildXML()
{
BuiltXML += "<ExpressMailLabelRequest USERID=" + user_id + "><Option/><EMCAAccount/>";
BuiltXML += "<EMCAPassword/><ImageParameters/>";
BuiltXML += Session["FromAddress"];
BuiltXML += Session["FromPhone"];
BuiltXML += Session["ToAddress"];
BuiltXML += Session["ToPhone"];
}
protected void ExpressShipLabel_Click(object sender, EventArgs e)
{
buildXML();
if (Flatrate.SelectedValue == "Yes")
{
BuiltXML += "<WeightInOunces>" + weight.Text + "</WeightInOunces>";
}
else
{
BuiltXML += "<FlatRate>True</FlatRate>";
}
if (waiver.SelectedIndex==0)
{
BuiltXML += "<WaiverOfSignature>True</WaiverOfSignature>";
}
if (Defer.SelectedIndex==1)
{
BuiltXML += "<NoWeekend>True</NoWeekend>";
}
else if (Defer.SelectedIndex==2)
{
BuiltXML += "<NoHoliday>True</NoHoliday>";
}
if (newZip.Text != "")
{
BuiltXML += "<POZipCode>+ newZip.Text+ </POZipCode>";
}
if (imaget.SelectedIndex== 1)
{
BuiltXML += "<ImageType>PDF</ImageType>";
}
BuiltXML += "</ExpressMailLabelRequest>";
retrieveLabel();
}//End ShipLable_Click
string response;
// Now sent it and get the information back for post
protected void retrieveLabel()
{
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(theURL + BuiltXML);
myReq.Method = "POST";
HttpWebResponse resp = (HttpWebResponse)myReq.GetResponse();
StreamReader loResponseStream = new StreamReader(resp.GetResponseStream());
response = loResponseStream.ReadToEnd();
resp.Close();
loResponseStream.Close();
bool iserror = IsResponseError();
if (!iserror)
{
Console.WriteLine( "Address Validated");
}
}
catch (Exception f)
{
Console.WriteLine(f.Message);
}
}//End retrieveLabel
private bool IsResponseError()
{
if (response.Contains("/error"))
{
Console.WriteLine("Please make sure to enter correct address information");
return true;
}
else { return false; }
}//End IsReponseError
}