开发者

Windows phone 7 - How to use HTTPWebRequest to POST / GET data from a .jsp site[with cookies]

开发者 https://www.devze.com 2023-02-18 02:51 出处:网络
Title sums it up fairly well. Said site has cookies, I need to post data from a textbox as a value on said site, and get one of two variables back. I was reading through some tutorials and a few Wind

Title sums it up fairly well.

Said site has cookies, I need to post data from a textbox as a value on said site, and get one of two variables back. I was reading through some tutorials and a few Windows phone 7 books. None of them were related to what I was trying to do. They only dealt with single who开发者_StackOverflowle files or something that could be made into a URL. I could also do it that way if someone had a way to also use cookies and just send it as a url [but i do not know how to construct the url in such a way to make that a realistic solution].


You should be able to send cookie's using code like:

CookieContainer container = new CookieContainer();
container.Add(new Uri("http://yoursite"), new Cookie("name", "value"));
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://yoursite");
request.CookieContainer = container;
request.BeginGetResponse(new AsyncCallback(GetData), request);

Code borrowed from HttpWebRequest and Set-Cookie header in response not parsed (WP7)

If the server is expecting to use cookies (e.g. for authentication) then there is no way that you'll be able to use form variables/query parameters instead.

You need to use a tool called Fiddler to inspect the calls that the website currently makes - this will include a mixture of:

  • cookie variables - especially for authentication
  • get variables - passed within the url path
  • and post variables - passed within the body of the request

If you do need to do a full POST, then you will need to set variables like - request.Method and request.ContentType - and you will need to brovide a RequestStream. There are libraries you can use like HAMMOCK to help - or I've got some example code in iron7 - see the DoCodePost method at the botom of this uploader class - or take a look at lots of other projects on CodePlex and GitHub.

0

精彩评论

暂无评论...
验证码 换一张
取 消