I have been trying to use the BingAds Soap API call to get a keyword performance. But the following request falled for some reason. I would appreciate if someone can tell me where tyhe problem is.It returns the Internal 500 error.
private void button1_Click(object sender, EventArgs e)
{
string xml = #"<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope
xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:v8='https://adcenter.microsoft.com/v8'
xmlns:arr='https://schemas.microsoft.com/2003/10/Serialization/Arrays'>
<soapenv:Header>
<v8:UserName>xxx</v8:UserName>
<v8:Password>xxx</v8:Password>
<v8:DeveloperToken>xxx</v8:DeveloperToken>
<v8:CustomerId>xxx</v8:CustomerId>
<v8:CustomerAccountId>xxx</v8:CustomerAccountId>
<v8:ApplicationToken></v8:ApplicationToken>
</soapenv:Header>
<soapenv:Body>
<v8:SubmitGenerateReportRequest>
<v8:ReportRequesti:type=""v8:KeywordPerformanceReportRequest""xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
<v8:Format>Csv</v8:Format>
<v8:Language>English</v8:Language>
<v8:ReportName>MyReport</v8:ReportName>
<v8:ReturnOnlyCompleteData>false</v8:ReturnOnlyCompleteData>
<v8:Aggregation>Daily</v8:Aggregation>
<v8:Columns>
<v8:KeywordPerformanceReportColumn>AccountId</v8:KeywordPerformanceReportColumn>
<v8:KeywordPerformanceReportColumn>AccountName</v8:KeywordPerformanceReportColumn>
<v8:KeywordPerformanceReportColumn>Keyword</v8:KeywordPerformanceReportColumn>
<v8:KeywordPerformanceReportColumn>KeywordId</v8:KeywordPerformanceReportColumn>
<v8:KeywordPerformanceReportColumn>AdGroupName</v8:KeywordPerformanceReportColumn>
<v8:KeywordPerformanceReportColumn>CostPerConversion</v8:KeywordPerformanceReportColumn>
</v8:Columns>
<v8:Scope>
<v8:AccountIds>
<arr:long>xxx</arr:long>
</v8:AccountIds>
</v8:Scope>
<v8:Time>
<v8:PredefinedTime>LastSevenDays</v8:PredefinedTime>
</v8:Time>
</v8:ReportRequest>
</v8:SubmitGenerateReportRequest>
</soapenv:Body>
</soapenv:Envelope>";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc");
req.ContentType = "text/xml; charset=utf-8";
req.Method = "POST";
req.Accept = "text/xml";
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(xml);
}
WebResponse response1 = req.GetResponse();
Stream responseStream = response1.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responseStream);
string responseData = myStreamReader.ReadToEnd();
}
AFAIK the Arrays namespace should not be https.
xmlns:arr='http://schemas.microsoft.com/2003/10/Serialization/Arrays'
Also you shouldn't need extra quotes around KeywordPerformanceReportRequest.
"v8:KeywordPerformanceReportRequest"
That should not impact the request, but is not necessary.
I hope this helps!
Eric Urban [MSFT Bing Ads UA]
This line:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc");
Shouldn't you have ?wsdl in the URL, like this:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://adcenterapi.microsoft.com/Api/Advertiser/v8/Reporting/ReportingService.svc?wsdl");
Related
I'm trying to do a WebRequest to a site from a Windows Phone Application.
But is vry important for me to also get the response from the server.
Here is my code:
Uri requestUri = new Uri(string.Format("http://localhost:8099/hello/{0}", metodo));
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
httpWebRequest.ContentType = "application/xml; charset=utf-8";
httpWebRequest.Method = "POST";
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
httpWebRequest.EndGetRequestStream, null))
{
string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Ahri</string>";
byte[] xmlAsBytes = Encoding.UTF8.GetBytes(xml);
await stream.WriteAsync(xmlAsBytes, 0, xmlAsBytes.Length);
}
Unfortunatelly, I have no idea of how I could get the response from the server.
Does anyone have an idea?
Thanks in advance.
Thanks to #max I found the solution and wanted to share it above.
Here is how my code looks like:
string xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Claor</string>";
Uri requestUri = new Uri(string.Format("http://localhost:8099/hello/{0}", metodo));
string responseFromServer = "no response";
HttpWebRequest httpWebRequest = HttpWebRequest.Create(requestUri) as HttpWebRequest;
httpWebRequest.ContentType = "application/xml; charset=utf-8";
httpWebRequest.Method = "POST";
using (Stream requestStream = await httpWebRequest.GetRequestStreamAsync())
{
byte[] xmlAsBytes = Encoding.UTF8.GetBytes(xml);
await requestStream.WriteAsync(xmlAsBytes, 0, xmlAsBytes.Length);
}
WebResponse webResponse = await httpWebRequest.GetResponseAsync();
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
responseFromServer = reader.ReadToEnd();
}
I hope it will help someone in the future.
This is very common question for people new in windows phone app
development. There are several sites which gives tutorials for the
same but I would want to give small answer here.
In windows phone 8 xaml/runtime you can do it by using HttpWebRequest or a WebClient.
Basically WebClient is a wraper around HttpWebRequest.
If you have a small request to make then user HttpWebRequest. It goes like this
HttpWebRequest request = HttpWebRequest.Create(requestURI) as HttpWebRequest;
WebResponse response = await request.GetResponseAsync();
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseContent = reader.ReadToEnd();
// Do anything with you content. Convert it to xml, json or anything.
}
Although this is a get request and i see that you want to do a post request, you have to modify a few steps to achieve that.
Visit this place for post request.
If you want windows phone tutorials, you can go here. He writes awesome tuts.
I am not getting the results that documentation says. I login the Buddy; created application; copy this URL and assign to url string; when I execute the program I am not getting results that are expected (status + Accesstoken) as documentation says. Can anyone please tell me if I am missing something as newbie to http calls. Its running on http requester but not on Poster firefox add-on!
Documentation
http://dev.buddyplatform.com/Home/Docs/Getting%20Started%20-%20REST/HTTP?
Code
string parameters = "{appid:'xxxxxx', appkey: 'xxxxxxx', platform: 'REST Client'}";
private async void SimpleRequest()
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
sw.WriteLine(parameters);
sw.Close();
response = (HttpWebResponse) await request.GetResponseAsync();
}
catch (Exception)
{ }
}
Using the HTTP requester add-on on Firefox, I successfully retrieved an access token so their API work.
In C# they provide a line of code to submit your appid and appkey, that might be the problem :
Buddy.Init("yourAppId", "yourAppKey");
My guess is you have to use their .NET SDK!
You can certainly use the REST API from raw REST the way you're doing, though the .NET SDK will handle some of the more complex details of changing service root. I ran your code using my own Buddy credentials and I was able to get JSON containing an Access Token back. You may need to read the response stream back as JSON to retrieve the access token. I used the following code to dump the JSON to the console:
request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
sw.WriteLine(parameters);
sw.Close();
response = (HttpWebResponse)await request.GetResponseAsync();
Console.WriteLine(await new StreamReader(response.GetResponseStream()).ReadToEndAsync());
Using Newtonsoft.Json I can parse out my accessToken like this:
Uri url = new Uri("https://api.buddyplatform.com/devices");
request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
request.ContentType = "application/json";
request.Method = "POST";
StreamWriter sw = new StreamWriter(await request.GetRequestStreamAsync());
sw.WriteLine(parameters);
sw.Close();
response = (HttpWebResponse)await request.GetResponseAsync();
var parsed = JsonConvert.DeserializeObject<IDictionary<string,object>>( (await new StreamReader(response.GetResponseStream()).ReadToEndAsync()));
var accessToken = (parsed["result"] as JObject).GetValue("accessToken").ToString();
Console.WriteLine(accessToken);
The 3.0 SDK does all of this for you while exposing the rest of the service through a thin REST wrapper, the migration guide for the 3.0 SDK should help with this.
I am trying to checkin to Foursquare using the API, I have obtained the oauth_token and am doing a POST request with the oauth_token. According to the documentation the endpoint I'm hitting is https://api.foursquare.com/v2/checkins/add. This however returns a 400 Bad Request message. This is my code in C#
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.foursquare.com/v2/checkins/add?oauth_token"+ oauth_token + "&venueId=" + venueId);
request.Method = "POST";
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
When I do the same in curl however, it posts a checkin and I get a json response back
curl --data "oauth_token=[oaut_token]&venueId=[venueId]" https://api.foursquare.com/v2/checkins/add
Ultimately what worked is the following:
using (WebClient wc = new WebClient())
{
System.Collections.Specialized.NameValueCollection reqparm = new System.Collections.Specialized.NameValueCollection();
reqparm.Add("oauth_token", oauth_token);
reqparm.Add("venueId", venueId);
byte[] responsebytes = wc.UploadValues(URI, "POST", reqparm);
string responsebody = Encoding.UTF8.GetString(responsebytes);
}
Thanks everyone for your help!
You should write your data to request input stream: HttpWebRequest.GetRequestStream()
I'm pretty RESTless right now because I keep getting incomplete responses from Amazon. I'm using the Product Advertising API, making one ItemLookup request to the server.
The C# code is pretty basic:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string resultString;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
resultString = sr.ReadToEnd();
}
The number of chars I recieve is 17408- pretty constant but I've seen something around 16k as well.
This is how it always ends:
...ount><CurrencyCode>EUR</CurrencyCode><FormattedPrice>EUR 11,33</FormattedPri
Is there something I don't know about HttpWebRequest or Amazon's API?
the url (don't care about the key) edit: bad idea, limit exceeded...
This worked for me:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Stream s = response.GetResponseStream();
using (StreamReader sr = new StreamReader(s))
{
s.Flush();
resultString = sr.ReadToEnd();
...
}
I am trying to connect to the SalesForce.com bulk API so I can do mass uploads of data from my application. I have read through the PDF documentation which emphasizes using CURL to make the POST requests. In keeping with the instructions, I have created a text file in XML format which is used for logging into the server.
Login.txt contents below:
<?xml version="1.0" encoding="utf-8" ?>
<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Body>
<n1:login xmlns:n1="urn:partner.soap.sforce.com">
<n1:username>my username</n1:username>
<n1:password>my password</n1:password>
</n1:login>
</env:Body>
</env:Envelope>
Here is what I'm trying to do in my code to make the login request:
XmlDocument XMLResponse = null;
HttpWebRequest httpRequest;
HttpWebResponse httpResponse = null;
Stream requestStream = null;
Stream responseStream = null;
XmlTextReader xmlReader;
httpRequest = HttpWebRequest)WebRequest.Create("https://login.salesforce.com/services/Soap/c/20.0");
try
{
byte[] bytes = File.ReadAllBytes(filename);
httpRequest.Method = "POST";
httpRequest.ContentLength = bytes.Length;
httpRequest.ContentType = "text/xml; charset=UTF-8";
httpRequest.Headers.Add("SOAPAction: login");
requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
responseStream = httpResponse.GetResponseStream();
xmlReader = new XmlTextReader(responseStream);
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlReader);
XMLResponse = xmldoc;
xmlReader.Close();
}
httpResponse.Close();
}
When this code executes I always get a 500 error. Does anyone have any experience in doing what I am attempting to do? Could you please provide me with some suggestions?
Thank you in advance.
For the login part, just download and import the partner WSDL and use the generated web service client. Otherwise, you'll want to update your code to read the response when it gets a 500 status code (the SOAP spec requires fault messages to use a 500 status code), the response body will give you more clues to the problem. I'd expect in this case you're getting an identity confirmation error, and you'll need to provide your api security token in addition to your password in the login request.
Generate the XML file to salesforce login
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<login xmlns="urn:enterprise.soap.sforce.com">
<username>username</username>
<password>password + token</password>
</login>
</soap:Body>
</soap:Envelope>
use the following c# code for login to salesforce
XmlDocument doc = new XmlDocument();
doc.LoadXml(str);
string uri = "https://login.salesforce.com/services/Soap/c/21.0";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Headers.Add("SOAPAction", "login");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";
stm = req.GetRequestStream();
doc.Save(stm);
stm.Close();
WebResponse resp = req.GetResponse();
stm = resp.GetResponseStream();
XmlDocument doc1 = new XmlDocument();
doc1.Load(stm);