Weird paypal api issue - c#

I just started using the Paypal API and I'm stuck on this problem.
I generate a paypal request in code and when I send it I get back the following.
TIMESTAMP=2011-05-16T01:26:37Z
CORRELATIONID=6d4327d15421f
ACK=Failure
L_ERRORCODE0=10001
L_SHORTMESSAGE0=Internal Error
L_LONGMESSAGE0=Timeout processing request
When I run through the debugger and copy the generated request url and paste it into my web browser, I get a success response....
I'm sending the request like this - c#
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
WebResponse response = req.GetResponse();
The same request, but one sent by code, and one copied to the browser produces to different results. Why is that?

I got same problem. When remove the
request.Method ="POST";
line, problem is solved.

Related

WebRequest.GetResponse(): read response, even on a 401?

I'm having trouble with the authorization piece of a third-party API. The API is shooting me a 401 in the HTTP Status Code, and more info in the response body.
var req = HttpWebRequest.Create(url);
req.Method = "GET";
var resp = req.GetResponse();
This is the code I typically use, but GetResponse() throws an exception on the 401 and I never get the response body.
Is there another implementation of this request I'm missing, or another way to do this in .NET so that I can get the response even when the request generates a 401?
Thanks.
web exception
The WebException contains a Response object that you can use to read the response body

Quickest way to get StatusCode while posting data

What i'm wanting to do it post data (ID=123456&start=true) and have it return the website status (200, 404, 401, etc.)
I don't want any other content except for the StatusCode, what is my best way of doing this?
I have been using
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create("http://example.com/test.php");
request.Method = "POST";
But it seems to download the whole page before it gives me the StatusCode.
I have also tried:
request.Method = "HEAD";
But that wont let me send any post data with it.

Adding Headers and Post data in RESTfull/HTTP Request in C#

I'm having problems with sending POST request in C# and it seems I misunderstood some HTTP basics. So basically I'm implementing RESTfull service client, which work as follows:
Make POST request with username/password and get token
Use this token in header (Authorization:TOKEN) while making other GET/POST/PUT requests
I use WebRequest to make GET requests (with Authorization header) and it's working. But when I use following code to make PUT requests, service is giving "Authentication failed - not logged in" message back:
String url = String.Format("{0}/{1}", AN_SERVER, app);
WebRequest theRequest = WebRequest.Create(url);
theRequest.Method = "POST";
theRequest.ContentType = "text/x-json";
theRequest.ContentLength = json.Length;
Stream requestStream = theRequest.GetRequestStream();
requestStream.Write(Encoding.ASCII.GetBytes(json), 0, json.Length);
requestStream.Close();
theRequest.Headers.Add("Authorization", authToken);
HttpWebResponse response = (HttpWebResponse)theRequest.GetResponse();
I must be making minor mistake (at least I hope so) while sending POST request. So what am I doing wrong?
Thanks.
Moving Headers before the request steam works (as per AI W's comment), because the request stream is adding the body.
The way webrequest is implemented internally, you need to finish the header before writing body, and once its in stream format, its ready to send.
If you look at the implementation of webrequest in reflector or some such decompiling tool, you'll be able to see the logic.
Hope this helps

HttpWebRequest with GET: 401 unauthorized

I found some problem with httpWebRequest, I've read all the same issues on other forums, but answers don't seem to work. My code:
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp;
wr.ContentType = "text/html; charset=UTF-8";
wr.Method = "GET";
wr.Credentials = new NetworkCredential("user", "password");
resp = (HttpWebResponse)wr.GetResponse();
The remote server returned an error: (401) Unauthorized.
Response says there's no auth token in cookies. I can receieve this token using my auth request with POST method. I even tried to put it to CookieContainer by "new Cookie ("authToken",token_value)". But the result is the same - error 401. Does anybody know the solution?
Thanx.
I use Zimbra web server, have an access to control it. .NET 4.0. My url is the path to .eml file I need to download. To specify the file I need to add some GET parameters: id and part. So the whole address looks like http://someserver.info/service/content/get?id=1&part=1
(Answered in the comments and question edits by OP. Moved here. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
The authorization token in Zimbra called ZM_AUTH_TOKEN so you need to put your authtoken in cookies like this:
wr.CookieContainer = new CookieContainer();
wr.CookieContainer.Add(new Uri(url), new Cookie("ZM_AUTH_TOKEN", rc.AuthToken));
You don't need to put the auth headers then, the request will work

C# REST client sending data using POST

I'm trying to send a simple POST request to a REST web service and print the response (code is below, mostly taken from Yahoo! developer documentation and the MSDN code snippets provided with some of the documentation). I would expect the client to send:
Request Method: POST (i.e. I expect $_SERVER['REQUEST_METHOD'] == 'POST' in PHP)
Data: foo=bar (i.e. $_POST['foo'] == 'bar' in PHP)
However, it seems to be sending:
Request Method: FOO=BARPOST
Data: (blank)
I know the API works as I've tested it with clients written in Python and PHP, so I'm pretty sure it must be a problem with my C#. I'm not a .NET programmer by trade so would appreciate any comments/pointers on how to figure out what the problem is - I'm sure it's something trivial but I can't spot it myself.
uri, user and password variables are set earlier in the code - they work fine with GET requests.
request = (HttpWebRequest) WebRequest.Create(uri);
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;
request.ContentType = "application/x-www-form-urlencoded";
string postData = "foo=bar";
request.ContentLength = postData.Length;
StreamWriter postStream = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
postStream.Write(postData);
postStream.Close();
response = (HttpWebResponse) request.GetResponse();
The REST API is written in PHP, and the $_POST array is empty on the server when using the C# client.
Eventually found the HttpWebRequest.PreAuthenticate property which seems to solve the problem if the code is edited like so:
request = (HttpWebRequest) WebRequest.Create(uri);
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(user, password);
request.Method = WebRequestMethods.Http.Post;
From the documentation I presume this forces authentication before the actual POST request is sent. I'm not sure why the class doesn't do this automatically (libraries for other languages make this process transparent, unless you explicitly turn it off), but it has solved the problem for me and may save someone else another 2 days of searching and hair-pulling.
For what it's worth, PreAuthenticate doesn't need to be set for GET requests, only POST, although if you do set it for a GET request everything will still work, but take slightly longer.

Categories

Resources