我抓“百姓网安卓app”的请求头是这样的
POST http://www.baixing.com/api/mobile/user.login/ HTTP/1.1
BAPI-USER-TOKEN:
udid: 382b07c277a4dcb8
APP-VERSION: 7.5.0
BAPI-APP-KEY: api_androidbaixing
BAPI-NONCE: 382b07c277a4dcb8
BAPI-HASH: 31713c34ace51df794b39e288ba87707
User-Agent: com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip
Host: www.baixing.com
Content-Length: 62
Connection: Keep-Alive
{"password":"347723","type":"mobile","identity":"18312854037"}
我用HttpHelper写的代码如下:
[C#] 纯文本查看 复制代码 private void button1_Click(object sender, EventArgs e)
{
HttpResult hr = null;
HttpHelper hh = new HttpHelper();
HttpItem hi = new HttpItem();
hi.URL = "http://www.baixing.com/api/mobile/user.login/";
hi.KeepAlive = true;
hi.UserAgent = "com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;";
hi.Header.Add("BAPI-USER-TOKEN", "");
hi.Header.Add("udid", "382b07c277a4dcb8");
hi.Header.Add("APP-VERSION", "7.5.0");
hi.Header.Add("BAPI-APP-KEY", "api_androidbaixing");
hi.Header.Add("BAPI-NONCE", "382b07c277a4dcb8");
hi.Header.Add("BAPI-HASH", "31713c34ace51df794b39e288ba87707");
hi.ContentType = "application/json; charset=utf-8";
// hi.Postdata = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
string body = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
hi.Header.Set(HttpRequestHeader.AcceptEncoding, "gzip");
hi.PostdataByte = postBytes;
hi.Timeout = 100000;
hi.ReadWriteTimeout = 30000;
hr = hh.GetHtml(hi);
string s = hr.Html;
}
得到的返回数据是 {"error":2007,"message":"","ext":null}
但是最终用下面的代码(参考了HttpHepler.cs的源代码)才得到正确的返回数据
[C#] 纯文本查看 复制代码 private void button1_Click(object sender, EventArgs e)
{
MakeRequests();
}
HttpWebResponse response = null;
private void MakeRequests()
{
if (Request_www_baixing_com(out response))
{
byte[] ResponseByte = GetByte();
if (ResponseByte != null && ResponseByte.Length > 0)
{
string str = encoding.GetString(ResponseByte); //@@@看过来@@@最终得到的返回数据,但是格式有点问题,不过最够判断登陆成功了
}
// response.Close();
}
}
private MemoryStream GetMemoryStream(Stream streamResponse)
{
MemoryStream _stream = new MemoryStream();
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = streamResponse.Read(buffer, 0, Length);
while (bytesRead > 0)
{
_stream.Write(buffer, 0, bytesRead);
bytesRead = streamResponse.Read(buffer, 0, Length);
}
return _stream;
}
private bool Request_www_baixing_com(out HttpWebResponse response)
{
response = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.baixing.com/api/mobile/user.login/");
request.Headers.Add("BAPI-USER-TOKEN", @"");
request.Headers.Add("udid", @"382b07c277a4dcb8");
request.Headers.Add("APP-VERSION", @"7.5.0");
request.Headers.Add("BAPI-APP-KEY", @"api_androidbaixing");
request.Headers.Add("BAPI-NONCE", @"382b07c277a4dcb8");
request.Headers.Add("BAPI-HASH", @"31713c34ace51df794b39e288ba87707");
request.UserAgent = "com.quanleimu.activity/7.5.0;OPPO;OPPO;OPPO+A53;5.1.1;720x1200;";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip");
request.Method = "POST";
request.ServicePoint.Expect100Continue = false;
string body = @"{""password"":""347723"",""type"":""mobile"",""identity"":""18312854037""}";
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(body);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();
response = (HttpWebResponse)request.GetResponse();
// string ss = response.
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse)e.Response;
else return false;
}
catch (Exception)
{
if (response != null) response.Close();
return false;
}
return true;
}
private byte[] GetByte()
{
byte[] ResponseByte = null;
MemoryStream _stream = new MemoryStream();
// GZIIP处理
if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
{
//开始读取流并设置编码方式
_stream = GetMemoryStream(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress));
}
else
{
//开始读取流并设置编码方式
_stream = GetMemoryStream(response.GetResponseStream());
}
// 获取Byte
ResponseByte = _stream.ToArray();
_stream.Close();
return ResponseByte;
}
private Encoding encoding = Encoding.Default;
请问飞哥,这到底是什么问题!
|