2011-11-06 73 views
0

我想用C#登录到网页Android Market。我花了整整一天的时间阅读关于HTTP请求和POST数据,但似乎没有任何工作。我可以做的是阅读包含Google登录表单的网页。但阅读页面后登录似乎是不可能的...如何使用C#登录Android市场?

任何人都可以给我一个提示如何做到这一点?

BTW:我已经试过代码如下所示:

string mail = "[email protected]"; 
string pw = "XXXXXXXXXX"; 

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create (@"https://market.android.com/publish"); 
// Set the Method property of the request to POST. 
request.Method = "POST"; 
// Create POST data and convert it to a byte array. 
string postData = String.Format ("Email={0}&Passwd={1}&signIn=Sign+in", mail, pw); 
byte[] byteArray = Encoding.UTF8.GetBytes (postData); 
// Set the ContentType property of the WebRequest. 
request.ContentType = "application/x-www-form-urlencoded"; 
// Set the ContentLength property of the WebRequest. 
request.ContentLength = byteArray.Length; 
// Get the request stream. 
Stream dataStream = request.GetRequestStream(); 
// Write the data to the request stream. 
dataStream.Write (byteArray, 0, byteArray.Length); 
// Close the Stream object. 
dataStream.Close(); 
// Get the response. 
WebResponse response = request.GetResponse(); 
// Display the status. 
Console.WriteLine (((HttpWebResponse)response).StatusDescription); 
// Get the stream containing content returned by the server. 
dataStream = response.GetResponseStream(); 
// Open the stream using a StreamReader for easy access. 
StreamReader reader = new StreamReader (dataStream); 
// Read the content. 
string responseFromServer = reader.ReadToEnd(); 
// Display the content. 
Console.WriteLine (responseFromServer); 
// Clean up the streams. 
reader.Close(); 
dataStream.Close(); 
response.Close(); 
+0

该网站可能会发送一个验证cookie,你必须按顺序使用后续请求继续验证。 – driis

回答

1

我最常做的:让live http headers for firefox,你可以记录你的浏览器发送请求。然后在代码中简单地重复。

例如,android marketplace和你的代码有很大不同。

实际发布到:https://accounts.google.com/ServiceLoginAuth

和帖子中的数据continue=https%3A%2F%2Fmarket.android.com%2Fpublish%2FHome&followup=https%3A%2F%2Fmarket.android.com%2Fpublish%2FHome&service=androiddeveloper&nui=1&dsh=&GALX=&pstMsg=1&dnConn=&timeStmp=&secTok=&Email=Passwd=&signIn=

1

你需要创建一个CookieContainer实例,并将其分配给每个请求CookieContainer财产。

这让网站知道你仍然登录将存储登录cookie

+0

好的,我补充说。与Ron的回答一起,下面的事情似乎现在开始工作。但现在谷歌给了我一个“关闭cookie功能,请打开它。”结果为我的要求。还有什么我需要考虑的吗? – Boris

+0

他们可能需要一个在请求登录页面时设置的cookie。 – SLaks

+0

你能详细说明一下吗?因为其实我以为这就是我正在做的事情? – Boris