Windows窗体应用程序无法在另一台PC上运行

时间:2015-04-01 23:03:47

标签: c#

我创建了一个Windows窗体应用程序,它转到一个网站并执行HTTPwebrequest POST,然后在webbrowser中显示结果。我在我编写程序的计算机上测试了它,它的工作方式与它应该完全一样。但是,当我从bin文件夹中获取.exe文件以及htmlagilitypack.dll并在另一台计算机上运行测试时,它会执行POST数据,但是在broswer中显示的网页只是默认登录页面。我在小提琴手上看了它,好像饼干还没有被设置到网页浏览器上。我怎样才能解决这个问题?我还在发布模式下编译了程序,并将文件传输到release文件夹中,结果相同。

在程序中,我用它来设置浏览器的cookie。

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);

是否必须对该dll文件执行某些操作?

编辑:我确定两台计算机都安装了4.5 NET。

        //get the cookie first
        CookieCollection cookies = new CookieCollection();
        CookieContainer cookiecontainer = new CookieContainer();
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.supremenewyork.com" + url);
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(cookies);

        //get the auth token
        using (StreamReader authreader = new StreamReader(request.GetResponse().GetResponseStream()))
        {
            source = authreader.ReadToEnd();
        }
        //need the auth token
        string token = Regex.Match(source, "authenticity_token.+?value=\"(.+?)\"").Groups[1].Value;

        //need the POST url
        string action = Regex.Match(source, "UTF-8.+?action=\"(.+?)\"").Groups[1].Value;

        //get the reponse from the server and save the cookies from the first request
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        cookies = response.Cookies;
        response.Close();

        string formparam = string.Format("utf8=%E2%9C%93&authenticity_token={0}&size={1}&commit=add to cart", token, sizechart[0]);
        HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create("http://www.supremenewyork.com"+ action);

        webreq.CookieContainer = new CookieContainer();
        webreq.CookieContainer = request.CookieContainer;
        //webreq.CookieContainer.Add(cookies); //recover the cookie first request
        webreq.Method = "POST"; //set a POST method
        webreq.Referer = "http://www.supremenewyork.com" + url;
        webreq.ContentType = "application/x-www-form-urlencoded";
        webreq.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
        webreq.KeepAlive = true;
        webreq.AllowAutoRedirect = true;

        byte[] bytes = Encoding.UTF8.GetBytes(formparam);
        webreq.ContentLength = bytes.Length;


        //write
        Stream postdata = webreq.GetRequestStream(); //open connection
        postdata.Write(bytes, 0, bytes.Length); //send the data
        postdata.Close();

        //get the final response from the server
        HttpWebResponse resp = (HttpWebResponse)webreq.GetResponse();
        cookies = resp.Cookies;
        response.Close();
        //Stream answer = resp.GetResponseStream();
        //StreamReader _answer = new StreamReader(webreq.GetResponse().GetResponseStream());
        //string reply = _answer.ReadToEnd();
        //need to check if item has been added to cart
        //richTextBox1.Text = reply;


        //check if item has been added to cart
        HttpWebRequest webreq2 = (HttpWebRequest)WebRequest.Create("http://www.supremenewyork.com/shop/cart");
        //webreq2.CookieContainer = new CookieContainer();
        webreq2.CookieContainer = webreq.CookieContainer;

        HttpWebResponse resp2 = (HttpWebResponse)webreq2.GetResponse();
        Stream answer2 = resp2.GetResponseStream();
        StreamReader _answer2 = new StreamReader(webreq2.GetResponse().GetResponseStream());
        string reply2 = _answer2.ReadToEnd();
        string item = textBoxkeyword.Text;
        string color = textBoxcolor.Text.ToLower();
        if(reply2.Contains(color))
        {
            //proceed to check out

            //update user
            appendtext(nDateTime + item + " " + color + " added to cart" );
            appendtext(nDateTime + "Please check out the item");

            HttpWebRequest webreq3 = (HttpWebRequest)WebRequest.Create("http://www.supremenewyork.com/checkout");
            //webreq3.CookieContainer = new CookieContainer();
            webreq3.CookieContainer = webreq2.CookieContainer;
            HttpWebResponse resp3 = (HttpWebResponse)webreq3.GetResponse();

            //webBrowser1.ScriptErrorsSuppressed = false;
            //RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main", true);
            //RegKey.SetValue("Display Inline Images", "yes");


            string cookie_string = "";
            foreach (Cookie cook in resp3.Cookies)
            {
                cookie_string += cook.ToString() + ";";
                InternetSetCookie("http://www.supremenewyork.com/checkout", cook.Name, cook.Value);


            }

            webBrowser1.Navigate("http://www.supremenewyork.com/checkout");

        }

Edit2:添加了所有代码

0 个答案:

没有答案