返回
基础
分类

该方法主要是使用CookieContainer来解决的

日期: 2020-03-16 15:32 浏览次数 : 145

private string HttpPost(string Url, string postDataStr)         {             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);             request.Method = "POST";             request.ContentType = "application/x-www-form-urlencoded";             request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);             request.CookieContainer = cookie;             Stream myRequestStream = request.GetRequestStream();             StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));             myStreamWriter.Write(postDataStr);             myStreamWriter.Close();               HttpWebResponse response = (HttpWebResponse)request.GetResponse();               response.Cookies = cookie.GetCookies(response.ResponseUri);             Stream myResponseStream = response.GetResponseStream();             StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));             string retString = myStreamReader.ReadToEnd();             myStreamReader.Close();             myResponseStream.Close();               return retString;         }           public string HttpGet(string Url, string postDataStr)         {             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);             request.Method = "GET";             request.ContentType = "text/html;charset=UTF-8";               HttpWebResponse response = (HttpWebResponse)request.GetResponse();             Stream myResponseStream = response.GetResponseStream();             StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));             string retString = myStreamReader.ReadToEnd();             myStreamReader.Close();             myResponseStream.Close();               return retString;         }

之前调用一个系统的rest接口,该接口的调用方式是先登录获取cookie,然后将cookie作为后续请求的凭证。无奈只有java和php的样例代码。在网上找了很多解决方案,都没有实际解决问题。最后还是皇天不负有心人,找到了方法并测试有效,特地留存以备以后使用。

C#https的post请求,发送的uri接收方接收的地址乱了 100C
public string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create; //request.AllowAutoRedirect = false; //禁止自动重定向 request.Method = "POST"; request.Headers.Add("AK", "c74f6e81f3854e928a4e2633b9d55a89"); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount; request.CookieContainer = cookie; //cookie信息由CookieContainer自行维护 Stream myRequestStream = request.GetRequestStream(); using (StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding))//gb2312 { myStreamWriter.Write; myStreamWriter.Close(); } HttpWebResponse response = null; try { this.SetCertificatePolicy(); response = (HttpWebResponse)request.GetResponse(); } catch (System.Exception ex) { throw; } //获取重定向地址 //string url1 = response.Headers["Location"]; if (response != null) { Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding; string retString = myStreamReader.ReadToEnd(); //MessageBox.Show(retString); myStreamReader.Close(); myResponseStream.Close(); return retString; } else { return "error"; //post请求返回为空 } }

C#模拟get和post

该方法主要是使用CookieContainer来解决的。感谢该作者的文章解决了我的问题。

 我发的:https://console-api-test.qibeitech.com/sign/productCheck/bindDevice?lockId=86812017491050&bikeId=http://ofo.so/plate/11000000&timestamp=1504487889209&sign=3c02b0dd1c2b5ba9df236b78e89c5916 他们接收的:AKc74f6e81f3854e928a4e2633b9d55a89bikeIdhttp://ofo.so/plate/11000000http://ofo.so/plate/11000000https://console-api-test.qibeitech.com/sign/productCheck/bindDevice?lockId86812017491050lockId86812017491050timestamp15044878892091504487889209dcc59608864511e79673163ea306a3 

转:

#region 同步通过POST方式发送数据

求帮助,小白一个,,之前没怎么做过网络通信的

///

/// 通过POST方式发送数据

必赢手机登录网址 ,///

/// url

/// Post数据

/// Cookie容器

///

public string SendDataByPost(string Url,string postDataStr,ref CookieContainer cookie)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);

if (cookie.Count == 0)

{

request.CookieContainer = new CookieContainer();

cookie = request.CookieContainer;

}

else

{

request.CookieContainer = cookie;

}

request.Method = "POST";

request.ContentType = "application/x-www-form-urlencoded";

request.ContentLength = postDataStr.Length;

Stream myRequestStream = request.GetRequestStream();

StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));

myStreamWriter.Write(postDataStr);

myStreamWriter.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream myResponseStream = response.GetResponseStream();

StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

string retString= myStreamReader.ReadToEnd();

myStreamReader.Close();

myResponseStream.Close();

return retString;

}

#endregion

#region 同步通过GET方式发送数据

///

/// 通过GET方式发送数据

///

/// url

/// GET数据

/// GET容器

///

public string SendDataByGET(string Url, string postDataStr, ref CookieContainer cookie)

{

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url +( postDataStr == "" ? "" : "?") + postDataStr);

if (cookie.Count == 0)

{

request.CookieContainer = new CookieContainer();

cookie = request.CookieContainer;

}

else

{

request.CookieContainer = cookie;

}

request.Method = "GET";

request.ContentType = "text/html;charset=UTF-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream myResponseStream = response.GetResponseStream();

StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));

string retString = myStreamReader.ReadToEnd();

myStreamReader.Close();

myResponseStream.Close();

return retString;

}

#endregion

操作代码

CookieContainer cc=new CookieContainer();

cc.Add(new System.Uri(" "), new Cookie("PHPSESSID", "xx"));

string content =  web.SendDataByGET(" cc);

MessageBox.Show(content);


知行办公,专业移动办公平台

【总监】十二春秋之,3483099@qq.com;

【Master】zelo,616701261@qq.com;

【运营】狼行天下,897221533@qq.com;

【产品设计】流浪猫,364994559@qq.com;

【体验设计】兜兜,2435632247@qq.com;

【iOS】淘码小工,492395860@qq.com;iMcG33K,imcg33k@gmail.com;

【Android】人猿居士,1059604515@qq.com;思路的顿悟,1217022114@qq.com;

【java】首席工程师MR_W,feixue300@qq.com;

【测试】土镜问道,847071279@qq.com;

【数据】喜乐多,42151960@qq.com;

【安全】保密,你懂的。